按ID删除表中的一行 [英] Delete a row from a table by id

查看:149
本文介绍了按ID删除表中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点问题。我有一些动态创建的表,每行都有一个id。我想删除id为x的行。



我尝试了通常的方法(removeChild),但它对表不起作用。

  function deleteRow(tableid,rowid)
{
document.getElementById(tableid).removeChild(document.getElementById(rowid ));
}

我得到的错误是:没有找到节点code:8



我也尝试过:

  function deleteRow(tbodyid,rowid) 
{
document.getElementById(tbodyid).removeChild(document.getElementById(rowid));
}

并得到相同的错误。



我不能使用 deleteRow()方法,因为那个需要行的索引,我更喜欢搜索id标记索引,删除(即使我没有找到其他解决方案...)。

解决方案

如何:

  function deleteRow(rowid)
{
var row = document.getElementById(rowid);
row.parentNode.removeChild(row);
}

而且,如果失败,这应该真的有效:

  function deleteRow(rowid)
{
var row = document.getElementById(rowid);
var table = row.parentNode;
while(table&& table.tagName!='TABLE')
table = table.parentNode;
if(!table)
return;
table.deleteRow(row.rowIndex);
}


I have a little problem. I have some dynamically created tables and each row has an id. I want to delete the row with the id "x".

I tried the usual method (removeChild) but it doesn't work for tables apparently.

function deleteRow(tableid, rowid)  
{   
      document.getElementById(tableid).removeChild(document.getElementById(rowid));  
}   

The error I get is: Node was not found" code: "8

I also tried this:

function deleteRow(tbodyid, rowid)   
{  
      document.getElementById(tbodyid).removeChild(document.getElementById(rowid));   
}   

and got the same error.

I can't use the deleteRow() method because that one needs the index of the row and I prefer not to search for the id mark the index then delete (even though if I don't find other solutions...).

解决方案

How about:

function deleteRow(rowid)  
{   
    var row = document.getElementById(rowid);
    row.parentNode.removeChild(row);
}

And, if that fails, this should really work:

function deleteRow(rowid)  
{   
    var row = document.getElementById(rowid);
    var table = row.parentNode;
    while ( table && table.tagName != 'TABLE' )
        table = table.parentNode;
    if ( !table )
        return;
    table.deleteRow(row.rowIndex);
}

这篇关于按ID删除表中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆