Html 使用 javascript 动态添加和删除行

Html add and delete rows dynamically with javascript

我想要一个 table 最初没有行,之后动态创建每一行并且能够删除每一行。 我得到 无法读取 属性 'childNodes' of undefined 请让我知道应该怎么做, 谢谢。

function addRow() {
  var table = document.getElementById("myTable");
  var row = table.insertRow(0);
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  cell1.innerHTML = "NEW CELL1";
  cell2.innerHTML = "NEW CELL2";
}

function deleteRow(tableID) {
  try {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;

    for (var i = 0; i < rowCount; i++) {
      var row = table.rows[i];
      var chkbox = row.cells[0].childNodes[0];

      table.deleteRow(i);
      rowCount--;
      i--;
    }
  } catch (e) {
    alert(e);
  }
}
<!DOCTYPE html>
<html>

<head>
  <style>
    table,
    td {
      border: 1px solid black;
    }
  </style>
</head>

<body>

  <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

  <table id="myTable">
    <TR>

    </TR>
  </table>
  <br>

  <button type="button" onclick="addRow()">Add</button>
  <button type="button" onclick="deleteRow('myTable')">Delete</button>

</body>

</html>

函数 deleteRow() 看起来具有误导性。当你把整个table传给删除。那么为什么不在您创建的每一行中添加一个按钮“删除”。如果你想删除所有行,请参阅修改,我已将你的函数 deleteRow(tableID) 更改为 deleteAllTableRows(tableID)

function deleteThisRow() {
  this.closest('tr').remove()
}

function addRow() {
  /* create a span element ("Delete") with an event listener "deleteThisRow" */
  let deleteButton = document.createElement('span');
  deleteButton.innerHTML = 'Delete';
  deleteButton.addEventListener('click', deleteThisRow);
  
  var table = document.getElementById("myTable");
  var row = table.insertRow(0);
  var cell0 = row.insertCell(0);
  var cell1 = row.insertCell(1);
  var cell2 = row.insertCell(2);
  cell0.innerHTML = "NEW CELL1";
  cell1.innerHTML = "NEW CELL2";
  cell2.append(deleteButton); // add the "Delete" button to each row
}


function deleteAllTableRows(tableID) {
  var rows = document.querySelectorAll('#' + tableID + ' tr ')
  rows.forEach( row => { 
    row.remove()
  });
}
table,
td {
  border: 1px solid black;
}
<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable"></table>
<br>

<button type="button" onclick="addRow()">Add</button>
<button type="button" onclick="deleteAllTableRows('myTable')">Delete all rows</button>

要删除所有行,只需从 table

中删除 html
function deleteRow(tableID) {
  let table =  document.getElementById(tableID)
   table.innerHTML = "";
} 

也许你应该改变

var chkbox = row.cells[0].childNodes[0];

进入:

var chkbox = row.childNodes[0].childNodes[0];
  1. tableID.innerHTML=""; , 是清除 table.
  2. 中所有元素的简单方法
  3. 如果table为空,则没有子节点。那么它是未定义的。

deleteRow() 方法从 table 中删除指定索引处的行。 也许你应该改变

for (var i = 0; i < rowCount; i++) {
  table.deleteRow(0);;
}

您只需像下面这样更改 deleteRow() 函数即可实现。而 row.cells[0].childNodes[0] 给你错误。 row.cells[0] 给你 HTML 代码,类似于这样 - <td>NEW CELL1</td>。那个HTML的childNode没有任何信息。所以,总是抛出错误。

function addRow() {
  var table = document.getElementById("myTable");
  var row = table.insertRow(0);
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  cell1.innerHTML = "NEW CELL1";
  cell2.innerHTML = "NEW CELL2";
}

function deleteRow(tableID) {
  document.getElementById(tableID).innerHTML = "";
}
<!DOCTYPE html>
<html>

<head>
  <style>
    table,
    td {
      border: 1px solid black;
    }
  </style>
</head>

<body>

  <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

  <table id="myTable">
    <tr>

    </tr>
  </table>
  <br>

  <button type="button" onclick="addRow()">Add</button>
  <button type="button" onclick="deleteRow('myTable')">Delete</button>

</body>
</html>