Get all rows in the “current” table, and not from child tables

后端 未结 4 1393
无人及你
无人及你 2021-01-07 20:45

How can you get all rows in a table without getting the rows in child tables?

var rows = $(\'tr\', tbl);

This will return ALL

相关标签:
4条回答
  • 2021-01-07 21:00
    var count = $('#tableID').rows;
    

    It works, because the selector will return a HTMLTableElement object.

    0 讨论(0)
  • 2021-01-07 21:10

    Probably:

    var rows = $("#tableid>tr");
    
    0 讨论(0)
  • 2021-01-07 21:17

    If you just want the count of rows, you can simply use:

    var table = document.getElementById('tableID');  
    table.rows.length;  
    

    Or you can use direct children selector:

    $('table > tbody > tr').each(function(index, tr) {} );
    
    0 讨论(0)
  • 2021-01-07 21:25
    var rows = $('#tblID > tbody > tr')
    

    The child selector will get the table's <tbody> element and consequently get the <tr> elements that are direct children of the table's tbody.

    If you already have a table object:

    var rows = $(tbl).find('> tbody > tr');
    

    Or:

    var rows = $(tbl).children('tbody').children('tr');
    

    Here is a working example.

    0 讨论(0)
提交回复
热议问题