问题描述
我可以为每个第4个'item'div样式。
I can style every 4th 'item' div like so
jQuery(".item:nth-child(4n)").addClass("fourth-item");
并且工作正常,但是我隐藏一些项目,显示一些其他,这种风格,但只有可见的每4个项目的样式。所以我有一个函数,将删除这个样式并重新应用它,但我需要在重新应用的风格,它只是每隔4个可见项目,而不是每4个项目。我知道:可见选择器,但不能看到链接它与第n子选择器正确,任何想法?
and that works fine, but then I hide some items, show some others and want to re-do this styling, but only styling every 4th item that is visible. So I have a function that will remove this styling and reapply it, but I need to specify in the reapplying of the style that it is only every 4th visible item, not every 4th item. I know the ":visible" selector but can't seen to chain it with the nth-child selector properly, any ideas?
我试过各种各样的事情,无效...
I've tried various things like this to no avail...
jQuery(".item").removeClass("fourth-item");
jQuery(".item:visible:nth-child(4n)").addClass("fourth-item");
推荐答案
扫描父项的子项,无论它们的样式是什么。在相对于父元素,而不是先前的选择器。这在的jQeury文档中解释:nth-child
:
使用更简单的方法,每个
/ p>
Using a more simple method with each
does exactly what you want:
$('#test li:visible').each(function (i) {
if (i % 4 == 0) $(this).addClass('fourth-item');
});
这篇关于jquery nth child是当前可见的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!