我有一个项目列表,我想从任何方向任何方向遍历.我还想在列表中循环,这意味着如果我最后列出最后一个列表项并单击下一个,我希望下一个项目成为列表中的第一个项目.我的页面很简单,只是一个5个项目的无序列表,2个按钮(一个用于前一个,一个用于下一个)和一个显示结果的textarea.这是我的代码:
$(function() {
var prev = $("#prev"); // grabbing the prev button
var next = $("#next"); // grabbing the next button
var listItems = $("#listItems p"); // grabbing the p elements of the list items
var results = $("#results"); // grabbing the textarea
var itemText; // variable for holding the text node
var selectedItem; // variable for the current selected item
listItems.click(function() { // click event for any item
itemText = $(this).text(); // selects the text of the clicked list …Run Code Online (Sandbox Code Playgroud)