我可以计算表格的行"$("this tbody tr").长度?

Jah*_*gir 7 each jquery this

我有3个具有相同类名的表table-sort.我想访问那些表.each()并计算tr内部tbody.

$("this tbody tr").length吗?

$('.table-sort').each(function(index) {
   var rowCount = $("this tbody tr").length; //not work , Could you please correct this?

   var rowCount1 = $(this).find('tbody > tr').length; //this is working fine
   alert(rowCount + '-' + rowCount1);
})
Run Code Online (Sandbox Code Playgroud)

Gab*_*oli 12

这是代码

$('.table-sort').each(function(index) {
   var rowCount = $("tbody tr", this).length; //will work now..

   var rowCount1 = $(this).find('tbody > tr').length; //this is working fine
   alert(rowCount + '-' + rowCount1);
})
Run Code Online (Sandbox Code Playgroud)

但是,你使用第二个代码,它的工作原理,应该是足够..


您还可以使用表DOM对象固有表属性

$('.table-sort').each(function(index) {
       var rowCount = this.rows.length;
    })
Run Code Online (Sandbox Code Playgroud)