jQuery jquery选择器计算可见表行的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2931893/
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
jquery selector to count the number of visible table rows?
提问by sprugman
I've got this html:
我有这个 html:
<table>
<tr style="display:table-row"><td>blah</td></tr>
<tr style="display:none"><td>blah</td></tr>
<tr style="display:none"><td>blah</td></tr>
<tr style="display:table-row"><td>blah</td></tr>
<tr style="display:table-row"><td>blah</td></tr>
</table>
I need to count the number of rows that don'thave display:none
. How can I do that?
我需要计算该行数不有display:none
。我怎样才能做到这一点?
回答by Nick Craver
You can use the :visible
selectorand .length
like this:
您可以使用:visible
选择器,.length
如下所示:
var numOfVisibleRows = $('tr:visible').length;
If the <table>
itself isn't visible on the screen (:visible
returns false if any parent is hidden, the element doesn't have to be hidden directly), then use .filter()
, like this:
如果<table>
本身在屏幕上不可见(:visible
如果隐藏了任何父元素,则返回 false,元素不必直接隐藏),然后使用.filter()
,如下所示:
var numOfVisibleRows = $('tr').filter(function() {
return $(this).css('display') !== 'none';
}).length;
回答by Tatu Ulmanen
$('tr:visible').length
$('tr:visible').length
回答by Kailas
You can also view particular table visible rows
您还可以查看特定的表可见行
var totalRow = $('#tableID tr:visible').length;
var totalRowWithoutHeader = totalRow-1;
The totalRowWithoutHeadergives the total row count excluding header row.
该totalRowWithoutHeader给总行数不包括标题行。
回答by Brian Mains
$("tr:visible") gets you the results of the visible rows, and I think you can then do .length
$("tr:visible") 为您提供可见行的结果,然后我认为您可以执行 .length