clear table jquery

后端 未结 10 1549
耶瑟儿~
耶瑟儿~ 2021-01-30 05:03

I have an HTML table filled with a number of rows.

How can I remove all the rows from the table?

相关标签:
10条回答
  • 2021-01-30 05:26
      $('#myTable > tr').remove();
    
    0 讨论(0)
  • 2021-01-30 05:29

    If you want to clear the data but keep the headers:

    $('#myTableId tbody').empty();
    

    The table must be formatted in this manner:

    <table id="myTableId">
        <thead>
            <tr>
                <th>header1</th><th>header2</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>data1</td><td>data2</td>
            </tr>
        </tbody>
    </table>
    
    0 讨论(0)
  • 2021-01-30 05:35

    The nuclear option:

    $("#yourtableid").html("");
    

    Destroys everything inside of #yourtableid. Be careful with your selectors, as it will destroy any html in the selector you pass!

    0 讨论(0)
  • 2021-01-30 05:40

    Slightly quicker than removing each one individually:

    $('#myTable').empty()
    

    Technically, this will remove thead, tfoot and tbody elements too.

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