jquery selector to count the number of visible tab

2019-01-22 17:04发布

I've got this 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't have display:none. How can I do that?

4条回答
Root(大扎)
2楼-- · 2019-01-22 17:15

You can also view particular table visible rows

 var totalRow =  $('#tableID tr:visible').length;
 var totalRowWithoutHeader = totalRow-1;

The totalRowWithoutHeader gives the total row count excluding header row.

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-22 17:25

$('tr:visible').length

查看更多
ら.Afraid
4楼-- · 2019-01-22 17:34

$("tr:visible") gets you the results of the visible rows, and I think you can then do .length

查看更多
forever°为你锁心
5楼-- · 2019-01-22 17:37

You can use the :visible selector and .length like this:

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:

var numOfVisibleRows = $('tr').filter(function() {
  return $(this).css('display') !== 'none';
}).length;
查看更多
登录 后发表回答