Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从div中删除子表_Javascript_Html - Fatal编程技术网

Javascript 从div中删除子表

Javascript 从div中删除子表,javascript,html,Javascript,Html,为了学习,我正在编写html代码和js脚本以供练习。 在这里,我想在每次单击generate按钮时生成一个新表。但由于我使用了AppendChild,每次单击都会追加表。我尝试过addchild(tbl1)和类似的方法,但它不起作用。你能指出我哪里做错了吗 function drawTable() { // get the reference for the body var div1 = document.getElementById('div1'); // crea

为了学习,我正在编写html代码和js脚本以供练习。 在这里,我想在每次单击generate按钮时生成一个新表。但由于我使用了AppendChild,每次单击都会追加表。我尝试过addchild(tbl1)和类似的方法,但它不起作用。你能指出我哪里做错了吗

function drawTable() {
    // get the reference for the body
    var div1 = document.getElementById('div1');

    // creates a <table> element
    var tbl = document.createElement("table");

    // creating rows
    for (var r = 0; r < totalRows; r++) {
        var row = document.createElement("tr");

     // create cells in row
         for (var c = 0; c < cellsInRow; c++) {
            var cell = document.createElement("td");
    getRandom = Math.floor(Math.random() * (max - min + 1)) + min;
            var cellText = document.createTextNode(Math.floor(Math.random() * (max - min + 1)) + min);
            cell.appendChild(cellText);
            row.appendChild(cell);
        }           

tbl.appendChild(row); // add the row to the end of the table body
    }
    div1.appendChild(tbl);
函数drawTable(){
//获取车身参考
var div1=document.getElementById('div1');
//创建一个元素
var tbl=document.createElement(“表”);
//创建行
对于(var r=0;r
在获取div1.while(div1.firstChild){div1.removeChild(div1.firstChild);}后添加此代码;是否要用新表替换现有表?@user8708感谢它的使用。您可以在
div1.innerHTML=“”
行后添加
var div1=document.getElementById('div1');
like