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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:34:40  来源:igfitidea点击:

jquery selector to count the number of visible table rows?

jqueryjquery-selectors

提问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 :visibleselectorand .lengthlike this:

您可以使用:visible选择器.length如下所示:

var numOfVisibleRows = $('tr:visible').length;

If the <table>itself isn't visible on the screen (:visiblereturns 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