<script>
function addRow()
{
var obj=document.getElementById('table1');
var insertRow = obj.insertRow(1);
var td=insertRow.insertCell();
td.innerHTML='add 1 row';
}
</script><table id="table1">
<tr id="tr1">
<td>this is the first line</td>
</tr>
<tr id="tr2">
<td>this is the second line</td>
</tr>
<a onclick="add()">插入一行</a>
function addRow()
{
var obj=document.getElementById('table1');
var insertRow = obj.insertRow(1);
var td=insertRow.insertCell();
td.innerHTML='add 1 row';
}
</script><table id="table1">
<tr id="tr1">
<td>this is the first line</td>
</tr>
<tr id="tr2">
<td>this is the second line</td>
</tr>
<a onclick="add()">插入一行</a>
<a onclick="addRow()">插入一行</a>
<script>
//tbody 是默认存在的。
function addRow()
{
var table = document.getElementById('table1');
var row = document.createElement('tr');
var cell = document.createElement('td');
cell.innerHTML = 'abcd';
row.appendChild(cell)
table.childNodes(0).appendChild(row);
}
</script>
<div>
<table id="table1">
<tr>
<td>1234</td>
</tr>
</table>
</div>
<input onclick="addRow();" type="button" value="Add Row">
The table.childNodes(0).appendChild(row), 比较奇怪,childNodes应该是rows吧,但虽然我有多个rows, 他还是只能调用childNodes(0), 不能childNodes(1), 所以他不能加到tr1, tr2之间。
先获得tr2的行号,然后tb.insertRow(tr2_rowIndex);就可以加在tr2的行前
var tab = document.getElementById("表名");
var row = tab.insertRow(); //新规一个行对象
var cell1 = row.insertCell(0);// 新规一个列对象
</script>
然后通过 cell1.insertHTML来添加具体内容,也可通过cell1.name等等设置属性