注:這意味着是一個社區維基職位我想一些行添加到HTML表,但它的失敗
使用簡單的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>
一個更深入的例子: http://stackoverflow.com/a/19561902/2536357 – tuned 2013-10-24 09:32:03