jQuery:给定一个选择器,仅查找其可见元素
这应该是一件容易的事。我已经声明了一个名为 $listItems 的变量。声明如下所示:
var $listItems = $ul.children('li'); // $ul is just a selected unordered list
稍后在我的代码中,我只想获取当前可见的内容。我该怎么办呢?比如:
$listItems.parent().children(':visible')?
谢谢。
This should be an easy one. I have a variable that I've already declared called $listItems. The declaration looks like this:
var $listItems = $ul.children('li'); // $ul is just a selected unordered list
Later in my code, I'd like to only get the ones that are currently visible. How would I go about that? Something like:
$listItems.parent().children(':visible')?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
.filter()
来缩小一组元素的范围仅那些与选择器(或函数)匹配的内容,如下所示:You can use
.filter()
to narrow down a set of elements to only those that match a selector (or a function), like this:您可以通过 :visible 选择器来获得它。它可以用在任何 jQuery 集合方法
$()
、filter()
、children()
、find() 中
等。注意:页面上可见的内容与设置了
visibility
属性的内容之间是有区别的。You have it with the :visible selector. It can be used in any of the jQuery collection methods
$()
,filter()
,children()
,find()
, etc.Note: There is a difference between something that is visible on the page and has its
visibility
property set.