用jQuery删除表行的最佳方法是什么?

本文翻译自: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”做某事,所以我不知道它是否有效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值