2011-06-08 107 views
0

我必须根据用户选择在两个表格之间进行切换选择我可以使用表格对象模型进行切换,但存在问题,因为我删除一个表格并加载另一个表格,但下一次当我切换回第一个表格时不会得到表格ID并且不能工作。我们可以删除dom表吗?

与DOM这样工作

var tbl1 = document.createElement("table"); 

tbl1.setAttribute('id','shorttable'); 

进行删除我用下面的Java脚本

var b = document.getElementById('shorttable'); 

document.removeChild(b); 

,但给我的异常

uncaught Error: NOT_FOUND_ERR: DOM Exception

,而有没有办法做到这一点使用DOM?

@lonesomeday谢谢你的答复,但它不工作时,我从其他函数调用它? 我正在尝试的是这样的。

<html> 
    <head> 
    <title>test dom</title> 
    <script> 

    function trackmode() 
{ 
    // i have to delete other table from this function where it is not working 
    var b = document.getElementById('shorttable'); 
    alert(b); 
    b.parentNode.removeChild(b); 

    var body = document.getElementsByTagName("body")[0]; 


    // creates a <table> element and a <tbody> element 
    var tbl  = document.createElement("table"); 
    tbl.setAttribute('id','tracktable'); 
    var tblBody = document.createElement("tbody"); 

    // creating all cells 
    for (var j = 0; j < 2; j++) { 
     // creates a table row 
     var row = document.createElement("tr"); 

     for (var i = 0; i < 2; i++) { 
      // Create a <td> element and a text node, make the text 
      // node the contents of the <td>, and put the <td> at 
      // the end of the table row 
      var cell = document.createElement("td"); 
      var cellText = document.createTextNode("cell is row "+j+", column "+i); 
      cell.appendChild(cellText); 
      row.appendChild(cell); 
     } 

     // add the row to the end of the table body 
     tblBody.appendChild(row); 
    } 

    // put the <tbody> in the <table> 
    tbl.appendChild(tblBody); 
    // appends <table> into <body> 
    body.appendChild(tbl); 
    // sets the border attribute of tbl to 2; 
    tbl.setAttribute("border", "2"); 
} 


function shortenmode() 
{ 

    var b = document.getElementById('tracktable'); 
b.parentNode.removeChild(b); 

    var body1 = document.getElementsByTagName("body")[0]; 
    // creates a <table> element and a <tbody> element 
    var tbl1 = document.createElement("table"); 
    tbl1.setAttribute('id','shorttable'); 
    var tblBody1 = document.createElement("tbody"); 

    // creating all cells 
    for (var j = 0; j < 2; j++) { 
     // creates a table row 
     var row = document.createElement("tr"); 

     for (var i = 0; i < 2; i++) { 
      // Create a <td> element and a text node, make the text 
      // node the contents of the <td>, and put the <td> at 
      // the end of the table row 
      var cell = document.createElement("td"); 
      var cellText = document.createTextNode("cell is row "+j+", column "+i); 
      cell.appendChild(cellText); 
      row.appendChild(cell); 
     } 

     // add the row to the end of the table body 
     tblBody1.appendChild(row); 
    } 

    // put the <tbody> in the <table> 
    tbl1.appendChild(tblBody1); 
    // appends <table> into <body> 
    body1.appendChild(tbl1); 
    // sets the border attribute of tbl to 2; 
    tbl1.setAttribute("border", "2"); 

     //here it will work 
    //var b = document.getElementById('shorttable'); 
    //alert(b); 
    //b.parentNode.removeChild(b); 

    //myP.removeChild(myTextNode); 
} 


    </script> 
    </head> 
    <body > 
<button type="button" name="website" onclick=trackmode() > track</button> 
<button type="button" name="website1" onclick=shortenmode() > short </button> 
</body> 
</html> 
+0

你能展示你的HTML并解释什么是未能发生? – lonesomeday 2011-06-08 10:54:36

+0

我试图删除当前表并按照按钮单击显示新的表。 – sagar 2011-06-08 11:00:05

+0

其工作..因为我在这里显示同一张表,这就是为什么我没有认出它 – sagar 2011-06-08 11:38:32

回答

5

要删除一个元素,你需要从它的母公司从文件中删除。所以:

b.parentNode.removeChild(b); 
相关问题