jQuery:如何计算“显示”不是“无”的元素数量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5325030/
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 18:58:07  来源:igfitidea点击:

jQuery: How to count the number of elements which "display" isn't "none"?

jquery

提问by Misha Moroshko

I use show()and hide()to show and hide rows in a table.

我使用show()hide()来显示和隐藏表格中的行。

How could I count the number of non-hidden rows (more accurately, rows with display!= none) ?

我如何计算非隐藏行的数量(更准确地说,是带有display!= 的行none)?

Note that:

注意:

$('tr:visible').length

won't work because if the table itself has display=none, the result will always be 0.

将不起作用,因为如果表本身有display=none,结果将始终为 0。

回答by Richard Dalton

Try this:

尝试这个:

$('tr:not([style*="display: none"])').length

Example http://jsfiddle.net/infernalbadger/7LvD5/

示例http://jsfiddle.net/infernalbadger/7LvD5/

回答by Alnitak

Filter your rows based on their actual CSS property:

根据行的实际 CSS 属性过滤行:

$('tr').filter(function() {
    return $(this).css('display') !== 'none';
}).length;