jQuery 删除具有特定 id 的表格行

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

remove table row with specific id

jquery

提问by John

I have the following table:

我有下表:

<table id="test">
 <tr id=1><td>bla</td></tr>
 <tr id=2><td>bla</td></tr>
 <tr id=3><td>bla</td></tr>
 <tr id=4><td>bla</td></tr>
</table>

Now I want to remove row 3 from the table. How do I do that? Something like:

现在我想从表中删除第 3 行。我怎么做?就像是:

$("#test tr ??").remove();

Thanks!

谢谢!

回答by Dave G

Try

尝试

$('table#test tr#3').remove();

回答by Andy E

ID attributes cannot start with a number and they should be unique. In any case, you can use :eq()to select a specific row using a 0-based integer:

ID 属性不能以数字开头,并且它们应该是唯一的。在任何情况下,您都可以使用:eq()从 0 开始的整数来选择特定行:

// Remove the third row
$("#test tr:eq(2)").remove();

Alternatively, rewrite your HTML so that it's valid:

或者,重写您的 HTML 以使其有效:

<table id="test">
 <tr id=test1><td>bla</td></tr>
 <tr id=test2><td>bla</td></tr>
 <tr id=test3><td>bla</td></tr>
 <tr id=test4><td>bla</td></tr>
</table>

And remove it referencing just the id:

并删除它只引用 id:

$("#test3").remove();

回答by andrew

Remove by id -

按 id 删除 -

$("#3").remove();

$("#3").remove();

Also I would suggest to use better naming, like row-1, row-2

另外我建议使用更好的命名,如 row-1, row-2

回答by Jon

Simply $("#3").remove();would be enough. But 3isn't a good id (I think it's even illegal, as it starts with a digit).

简单$("#3").remove();就足够了。但3不是一个好的 id(我认为它甚至是非法的,因为它以数字开头)。

回答by GolezTrol

$('#3').remove();

Might not work with numeric id's though.

不过可能不适用于数字 ID。

回答by David

Try:

尝试:

$("#test tr:eq(2)").remove();

回答by Fermin

Use the :eqselector:

使用:eq选择器:

$("#test tr:eq(2)").remove();