jQuery: count number of rows in a table

How do I count the number of tr elements within a table using jQuery?

I know there is a similar question, but I just want the total rows.


Use a selector that will select all the rows and take the length.

var rowCount = $('#myTable tr').length;

Note: this approach also counts all trs of every nested table!


如果您在表中使用<tbody><tfoot> ,则必须使用以下语法,否则您将得到不正确的值:

var rowCount = $('#myTable >tbody >tr').length;

Alternatively...

var rowCount = $('table#myTable tr:last').index() + 1;

jsFiddle DEMO

This will ensure that any nested table-rows are not also counted.

链接地址: http://www.djcxy.com/p/83806.html

上一篇: 未被KnockoutJS捕获的jQuery UI日期选择器更改事件

下一篇: jQuery:统计表中的行数