本文翻译自:What is the best way to remove a table row with jQuery?
用jQuery删除表行的最佳方法是什么?
#1楼
参考:https://stackoom.com/question/Iu1/用jQuery删除表行的最佳方法是什么
#2楼
If the row you want to delete might change you can use this. 如果您要删除的行可能会更改,则可以使用此行。 Just pass this function the row # you wish to delete. 只需将此函数传递到要删除的行#即可。
function removeMyRow(docRowCount){
$('table tr').eq(docRowCount).remove();
}
#3楼
if you have HTML like this 如果您有这样的HTML
<tr>
<td><span class="spanUser" userid="123"></span></td>
<td><span class="spanUser" userid="123"></span></td>
</tr>
where userid="123"
is a custom attribute that you can populate dynamically when you build the table, 其中userid="123"
是自定义属性,您可以在构建表时动态填充该属性,
you can use something like 您可以使用类似
$(".spanUser").live("click", function () {
var span = $(this);
var userid = $(this).attr('userid');
var currentURL = window.location.protocol + '//' + window.location.host;
var url = currentURL + "/Account/DeleteUser/" + userid;
$.post(url, function (data) {
if (data) {
var tdTAG = span.parent(); // GET PARENT OF SPAN TAG
var trTAG = tdTAG.parent(); // GET PARENT OF TD TAG
trTAG.remove(); // DELETE TR TAG == DELETE AN ENTIRE TABLE ROW
} else {
alert('Sorry, there is some error.');
}
});
});
So in that case you don't know the class or id of the TR
tag but anyway you are able to delete it. 因此,在这种情况下,您不知道TR
标签的类或ID,但是无论如何您都可以删除它。
#4楼
是否可以接受以下内容:
$('#myTableRow').remove();
#5楼
You're right: 你是对的:
$('#myTableRow').remove();
This works fine if your row has an id
, such as: 如果您的行具有id
,则此方法很好用,例如:
<tr id="myTableRow"><td>blah</td></tr>
If you don't have an id
, you can use any of jQuery's plethora of selectors . 如果没有id
,则可以使用jQuery的大量选择器中的任何一个。
#6楼
function removeRow(row) {
$(row).remove();
}
<tr onmousedown="removeRow(this)"><td>Foo</td></tr>
Maybe something like this could work as well? 也许这样的事情也可以工作? I haven't tried doing something with "this", so I don't know if it works or not. 我没有尝试使用“ this”做某事,所以我不知道它是否有效。