看看:http://jsfiddle.net/dmhsfds4/
基本上,索引始终给出 -1,即使函数运行时类 .current
中存在元素。
这是一个错误吗?
<p class="current" id="one">Point 1</p>
<p id="two">Point 2</p>
<p id="three">Point 3</p>
<p id="four">Point 4</p>
<p id="five">Point 5</p>
<p id="six">Point 6</p>
<p id="seven">Point 7</p>
<小时/>
var p = $('p');
var i = 0;
setInterval(function(){
i +=1;
if (i>6) i=0;
console.log(p.index('.current'));
p.removeClass('current').eq(i).addClass('current');
},1000);
最佳答案
您需要将 p.index()
更改为 $('p.current').index()
。 p.index()
只是检查第一个 p
而不是每一个。
这是fiddle .
关于javascript - jquery Index() 无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31192380/