开发者

How do I count list elements that are not hidden?

开发者 https://www.devze.com 2022-12-25 23:29 出处:网络
Starting with a simple list: <ul> <li>Item 1</li> <li style=\"display: none;\">Item 2</li>

Starting with a simple list:

<ul>
    <li>Item 1</li>
    <li style="display: none;">Item 2</li>
    <li>Item 3</li>
</ul>

I know that I can subtract the hidden elements from the list total

$('ul li').size() -开发者_如何学Go $('ul li:hidden').size()

But I thought there might be a more elegant way to achieve this with jquery:

$('ul li:hidden:not').size()

That doesn't work. Any ideas?


The opposite of :hidden is :visible - jQuery docs.

$('ul li:visible').size()


The simplest form is:

var hidden = $("ul > li:hidden").length;

On a side note, to correctly use :not():

var hidden = $("ul > li:not(:visible)").length;

Lastly a jQuery object supports the size() method and the length property, which are interchangeable.

0

精彩评论

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

关注公众号