如何使用jQuery计算表中的tr元素的数量?

我知道有一个类似的问题,但我只想知道总行数。


当前回答

以下是我的看法:

//Helper function that gets a count of all the rows <TR> in a table body <TBODY>
$.fn.rowCount = function() {
    return $('tr', $(this).find('tbody')).length;
};

用法:

var rowCount = $('#productTypesTable').rowCount();

其他回答

以下是我的看法:

//Helper function that gets a count of all the rows <TR> in a table body <TBODY>
$.fn.rowCount = function() {
    return $('tr', $(this).find('tbody')).length;
};

用法:

var rowCount = $('#productTypesTable').rowCount();

如果在表中使用<tbody>或<tfoot>,则必须使用以下语法,否则将得到错误的值:

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

使用选择器选择所有行并取长度。

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

注意:这种方法也会计算每个嵌套表的所有trs !

如果有人的话,试试这个

没有头

$("#myTable > tbody").children.length

如果有头,那么

$("#myTable > tbody").children.length -1

享受! !

文件rows getElementById()“myTable。长度正好;