jquery nth child是当前可见的 [英] jquery nth child that is currently visible

查看:104
本文介绍了jquery nth child是当前可见的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以为每个第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");


推荐答案

:nth-​​child 扫描父项的子项,无论它们的样式是什么。在 :nth-​​child 相对于父元素,而不是先前的选择器。这在的jQeury文档中解释:nth-​​child


code>:nth-​​child(n),所有子项都被计数,而不管它们是什么,并且指定的元素只有当它与附加到伪类的选择器匹配时才被选择。

With :nth-child(n), all children are counted, regardless of what they are, and the specified element is selected only if it matches the selector attached to the pseudo-class.

使用更简单的方法,每个 / 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是当前可见的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆