开发者

jQuery: Given a selector, find only its visible elements

开发者 https://www.devze.com 2023-01-22 17:07 出处:网络
This should be an easy one. I have a variable that I\'ve already declared called $listItems. The declaration looks like this:

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 j开发者_如何学Pythonust 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.


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

$listItems.filter(':visible')


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消