jQuery选择器来计算可见表行的数量? [英] jquery selector to count the number of visible table rows?
本文介绍了jQuery选择器来计算可见表行的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个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>
我需要计算不具有display:none
的行数.我该怎么办?
I need to count the number of rows that don't have display:none
. How can I do that?
推荐答案
您可以使用 :visible
选择器和 .length
像这样:
You can use the :visible
selector and .length
like this:
var numOfVisibleRows = $('tr:visible').length;
如果<table>
本身在屏幕上不可见( :visible
如果隐藏了任何父项,则返回false,而不必直接隐藏该元素),然后使用 .filter()
,就像这样:
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:
var numOfVisibleRows = $('tr').filter(function() {
return $(this).css('display') !== 'none';
}).length;
这篇关于jQuery选择器来计算可见表行的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文