我对 jquery 中的 for 循环有点吃力。我有一个类为 .row
的 div。在该 div 中有许多 .item
div。事先不知道元素的数量。
我想要的是从 0 开始计数并在 3 结束。喜欢 (0,1,2,3,0,1,2,3...等)
HTML:
<div class="row cl">
<div class="item"></div>
</div>
我尝试过的:
$('.row > .item').each(function(i){
for(i=0;i<4;++i){
// Here i want to add a class (repeatedly) if the index of the .item is 3.
}
});
上述示例中的结果无法正常工作。我到处都是3。我知道如何添加一个与计数部分有关的类。
I normally can use a CSS selector like
:nth-child()
but i need it to work in IE 8, and that's not supporting all of the child selectors.
最佳答案
有些答案错得离谱
$('.row > .item').each(function(i){
if (i % 4 == 3)
// Add class.
$(this).addClass("newclass");
});
晚安
关于javascript - 对于循环 jQuery,在 4 项之后重新开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32991335/