使用特定id删除表行

时间:2021-03-22 16:52:40

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!

谢谢!

8 个解决方案

#1


61  

Try

试一试

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

#2


14  

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();

#3


5  

Remove by id -

删除id -

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

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

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

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

#4


4  

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

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

#5


3  

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

http://api.jquery.com/remove/

http://api.jquery.com/remove/

#6


1  

Try:

试一试:

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

#7


1  

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

Might not work with numeric id's though.

可能不能使用数字id。

#8


0  

Use the :eq selector:

使用:情商选择器:

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

#1


61  

Try

试一试

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

#2


14  

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();

#3


5  

Remove by id -

删除id -

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

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

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

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

#4


4  

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

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

#5


3  

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

http://api.jquery.com/remove/

http://api.jquery.com/remove/

#6


1  

Try:

试一试:

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

#7


1  

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

Might not work with numeric id's though.

可能不能使用数字id。

#8


0  

Use the :eq selector:

使用:情商选择器:

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