我使用 JavaScript 来更改使用 JS 创建的表中某些单元格的内容,使用下面的行:
document.getElementById("tableID").rows[x].cells[y].innerHTML = "someValue";
但总是收到错误消息:(Uncaught TypeError:无法读取未定义的属性“单元格”),那么出了什么问题?
btnSave.onclick = function() {
var table = document.getElementById("table");
var row = table.insertRow(document.getElementById("table").rows.length);
var cellID = row.insertCell(0);
var cellNAME = row.insertCell(1);
var cellAGE = row.insertCell(2);
cellID.innerHTML = " some id ";
cellNAME.innerHTML = " some name ";
cellAGE.innerHTML = " some age ";
}
edit.onclick = function() {
var x;
for (x = 1; x <= document.getElementById("table").rows.length; x++) {
document.getElementById("table").rows[x].cells[0].innerHTML = x;
}
}
<button id="btnSave">Save</button>
<button id="edit">edit</button>
<table id="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</table>
翻阅古今
相关分类