【问题标题】:Can I count row of a table " $("this tbody tr").length?我可以计算表格的行“$(“this tbody tr”).length吗?
【发布时间】:2011-05-29 17:47:19
【问题描述】:

我有 3 个具有相同类名 table-sort 的表。我想通过.each() 访问这些表并计算tbody 中的tr

$("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);
})

【问题讨论】:

    标签: jquery this each


    【解决方案1】:

    这里是代码

    $('.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);
    })
    

    但是您使用的第二个代码,有效,应该足够了..


    您也可以使用inherent table properties of the table DOM object

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

    【讨论】:

    • 感谢 Gaby,其惊人的快速回答。
    猜你喜欢
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 2014-06-19
    • 1970-01-01
    相关资源
    最近更新 更多