【发布时间】:2011-08-25 14:17:20
【问题描述】:
注意:这是一个社区 wiki 帖子
以下代码使用简单的 dom 方法无法将行添加到表中。有什么问题?
<html>
<head>
<title>Javascript Test</title>
<script>
function addRow() {
var mytable = document.getElementById('mytable');
var row = document.createElement('tr');
var cell = document.createElement('td');
var text = document.createTextNode('This is a row');
cell.appendChild(text);
row.appendChild(cell);
mytable.appendChild(row);
}
</script>
</head>
<body>
<form action="#">
<table id="mytable">
<tr>
<td>This is a row</td>
</tr>
</table>
<input type="button" onclick="addRow()" value="Add A Row"/>
</form>
</body>
</html>
【问题讨论】:
-
一个更深入的例子:stackoverflow.com/a/19561902/2536357
标签: javascript html dom html-table