jQuery 清除表jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2620181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:53:30  来源:igfitidea点击:

clear table jquery

jqueryhtml-table

提问by learning

I have an HTML tablefilled with a number of rows.

我有一个填充了多行的HTML 表

How can I removeall the rows from the table?

如何从表中删除所有行?

回答by rahul

Use .remove()

使用.remove()

$("#yourtableid tr").remove();

If you want to keep the data for future use even after removing it then you can use .detach()

如果您想保留数据以备将来使用,即使在删除之后也可以使用.detach()

$("#yourtableid tr").detach();

If the rows are children of the table then you can use child selector instead of descendant selector, like

如果行是表的子行,则可以使用子选择器而不是后代选择器,例如

$("#yourtableid > tr").remove();

回答by HoffZ

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>

回答by nickf

Slightly quicker than removing each one individually:

比单独删除每个要快一点:

$('#myTable').empty()

Technically, this will remove thead, tfootand tbodyelements too.

从技术上讲,这也将删除thead,tfoottbody元素。

回答by Bumptious Q Bangwhistle

I needed this:

我需要这个:

$('#myTable tbody > tr').remove();

It deletes all rows except the header.

它删除除标题之外的所有行。

回答by KevinDeus

The nuclear option:

核选项:

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

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

破坏里面的一切#yourtableid。小心你的选择器,因为它会破坏你传递的选择器中的任何html!

回答by Mrinmoy Sen

$("#employeeTable td").parent().remove();

This will remove all trhaving tdas child. i.e all rows except the header will be deleted.

这将删除所有trtd作为的孩子。即除标题外的所有行都将被删除。

回答by James Cooke

This will remove all of the rows belonging to the body, thus keeping the headers and body intact:

这将删除属于正文的所有行,从而保持标题和正文完整:

$("#tableLoanInfos tbody tr").remove();

回答by miragessee

<table id="myTable" class="table" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
        </tr>
    </thead>
    <tbody id="tblBody">

    </tbody>
</table>

And Remove:

并删除:

$("#tblBody").empty();

回答by saidesh kilaru

  $('#myTable > tr').remove();

回答by hichamkazan

Having a table like this (with a header and a body)

有一个这样的表(带有标题和正文)

<table id="myTableId">
    <thead>
    </thead>
    <tbody>
   </tbody>
</table>

remove every tr having a parent called tbody inside the #tableId

删除每个在 #tableId 中有一个名为 tbody 的父项的 tr

$('#tableId tbody > tr').remove();

and in reverse if you want to add to your table

如果你想添加到你的表格中,反之亦然

$('#tableId tbody').append("<tr><td></td>....</tr>");