jQuery:给定一个选择器,只找到它的可见元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4048086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:34:19  来源:igfitidea点击:

jQuery: Given a selector, find only its visible elements

jqueryjquery-selectorshidden

提问by MegaMatt

This should be an easy one. I have a variable that I've already declared called $listItems. The declaration looks like this:

这应该是一件容易的事。我有一个已经声明为 $listItems 的变量。声明如下所示:

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.

谢谢。

回答by Nick Craver

You can use .filter()to narrow down a set of elements to only those that match a selector (or a function), like this:

您可以使用.filter()将一组元素缩小到仅匹配选择器(或函数)的元素,如下所示:

$listItems.filter(':visible')

回答by Jason McCreary

You have it with the :visibleselector. It can be used in any of the jQuery collection methods $(), filter(), children(), find(), etc.

您可以使用:visible选择器。它可以在任何的jQuery的收集方法使用$()filter()children()find(),等。

Note:There is a difference between something that is visibleon the page and has its visibilityproperty set.

注意:页面上可见的内容与visibility设置其属性的内容之间存在差异。