我有一个列表,其中包含测试用途的值,只有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>
答案 0 :(得分:0)
你可以这样做:
var current = 1;
function getnext(){
current++;
if (current > MAX_COUNT_HERE) {
current = 1;
}
var el = document.getElementById(current);
alert(el.innerHTML);
}
答案 1 :(得分: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
}