jQuery jQuery删除除第一个之外的所有表行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/370013/
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 09:02:39  来源:igfitidea点击:

jQuery delete all table rows except first

jqueryjquery-selectors

提问by Ken Paul

Using jQuery, how do I delete all rows in a table except the first? This is my first attempt at using index selectors. If I understand the examples correctly, the following should work:

使用 jQuery,如何删除表中除第一行之外的所有行?这是我第一次尝试使用索引选择器。如果我正确理解这些示例,则以下内容应该有效:

$(some table selector).remove("tr:gt(0)");

which I would read as "Wrap some table in a jQuery object, then remove all 'tr' elements (rows) where the element index of such rows is greater than zero". In reality, it executes without generating an error, but doesn't remove any rows from the table.

我将其读作“在 jQuery 对象中包装一些表,然后删除所有 'tr' 元素(行),其中这些行的元素索引大于零”。实际上,它执行时不会产生错误,但不会从表中删除任何行。

What am I missing, and how do I fix this? Of course, I could use straight javascript, but I'm having so much fun with jQuery that I'd like to solve this using jQuery.

我错过了什么,我该如何解决这个问题?当然,我可以直接使用 javascript,但是我对 jQuery 非常感兴趣,所以我想使用 jQuery 来解决这个问题。

回答by Strelok

This should work:

这应该有效:

$(document).ready(function() {
   $("someTableSelector").find("tr:gt(0)").remove();
});

回答by tvanfosson

I think this is more readable given the intent:

鉴于意图,我认为这更具可读性:

$('someTableSelector').children( 'tr:not(:first)' ).remove();

Using children also takes care of the case where the first row contains a table by limiting the depth of the search.

使用子项还可以通过限制搜索深度来处理第一行包含表格的情况。

If you had an TBODY element, you can do this:

如果你有一个 TBODY 元素,你可以这样做:

$("someTableSelector > tbody:last").children().remove();

If you have THEAD or TFOOT elements you'll need to do something different.

如果您有 THEAD 或 TFOOT 元素,则需要做一些不同的事情。

回答by jpmorin

Another way to accomplish this is using the empty() function of jQuery with the thead and tbody elements in your table.

实现此目的的另一种方法是将 jQuery 的 empty() 函数与表中的 thead 和 tbody 元素一起使用。

Example of a table:

表格示例:

<table id="tableId">
<thead>
    <tr><th>Col1</th><th>Col2</th></tr>
</thead>
<tbody>
    <tr><td>some</td><td>content</td></tr>
    <tr><td>to be</td><td>removed</td></tr>
</tbody>
</table>

And the jQuery command:

和 jQuery 命令:

$("#tableId > tbody").empty();

This will remove every rows contained in the tbody element of your table and keep the thead element where your header should be. It can be useful when you want to refresh only the content of a table.

这将删除表格的 tbody 元素中包含的每一行,并将 thead 元素保留在您的标题应该所在的位置。当您只想刷新表的内容时,它会很有用。

回答by Dave Ward

If it were me, I'd probably boil it down to a single selector:

如果是我,我可能会把它归结为一个选择器:

$('someTableSelector tr:not(:first)').remove();

回答by CMPalmer

Your selector doesn't need to be inside your remove.

你的选择器不需要在你的 remove 里面。

It should look something like:

它应该看起来像:

$("#tableID tr:gt(0)").remove();

Which means select every row except the first in the table with ID of tableID and remove them from the DOM.

这意味着选择表中除第一行之外的每一行,ID 为 tableID,并将它们从 DOM 中删除。

回答by Makubex

$('#table tr').slice(1).remove();

I remember coming across that 'slice' is faster than all other approaches, so just putting it here.

我记得遇到那个“切片”比所有其他方法都快,所以把它放在这里。

回答by Sanket Utekar

Consider a table with id tbl: the jQuery code would be:

考虑一个带有 id 的表tbl:jQuery 代码将是:

$('#tbl tr:not(:first)').remove();

回答by Biki

To Remove all rows, except the first one (except header), use the below code:

要删除除第一行以外的所有行(标题除外),请使用以下代码:

$("#dataTable tr:gt(1)").remove();

$("#dataTable tr:gt(1)").remove();

回答by Pytholabs

Easiest way :

最简单的方法:

$("#mytable").find($("tr")).slice(1).remove()

-first reference the table
-get the list of elements and slice it and remove the selected elements of the list

- 首先引用表 -
获取元素列表并将其切片并删除列表中的选定元素

回答by PodTech.io

wrapped in a function:

包裹在一个函数中:

function remove_rows(tablename) { 
    $(tablename).find("tr:gt(0)").remove();        
}

then call it:

然后调用它:

remove_rows('#table1');
remove_rows('#table2');