1

我有一个列表,其中包含用于测试目的的值,它只有 1-4 ..现在,当我单击按钮时,我想获得该列表中的下一个。现在我假设我必须将我当前所在的变量存储在一个变量中?

var current = 1;
function getnext(){
alert($('#list li').next().attr('id'));
    //update current with next value from list
}

<input type="button" onclick="getnext();" value="next" />
<ul id="list">
<li id="1"></li>
    <li id="2"></li>
    <li id="3"></li>
    <li id="4"></li>
</ul>
4

2 回答 2

0

像这样?

var current = 0;
var listItems = $('#list li');
function getnext(){
    //check if you haven't reached the end of the list yet...
    if(current < listItems.length()) {
      alert(listItems[current]);
      current++;
    }
    //update current with next value from list
}
于 2013-02-25T22:07:50.807 回答
0

你可以这样做:

var current = 1;
function getnext(){
   current++;
   if (current > MAX_COUNT_HERE) {
     current = 1;
   }
   var el = document.getElementById(current);
   alert(el.innerHTML);
}
于 2013-02-25T22:06:21.510 回答