根据用户的选择,我必须在两个表之间切换,我可以使用表对象模型进行切换,但当我删除一个表并加载另一个表时,它会出现问题,但下次当我切换回第一个表时,它将无法获取表id,也无法工作。
像这样使用DOM时

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

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

对于delete,我使用下面的java脚本
var b = document.getElementById('shorttable');

document.removeChild(b);

但是给我一个例外
未捕获错误:未找到\u错误:DOM异常
使用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>

最佳答案

要删除元素,需要从其父元素中删除它,而不是从文档中删除它。所以:

b.parentNode.removeChild(b);

10-04 15:31
查看更多