HTML DOM insertRow() 方法
定義和用法
insertRow() 方法用于在表格中的指定位置插入一個新行。
語法
tableObject.insertRow(index)
返回值
返回一個 TableRow,表示新插入的行。
說明
該方法創(chuàng)建一個新的 TableRow 對象,表示一個新的 <tr> 標(biāo)記,并把它插入表中的指定位置。
新行將被插入 index 所在行之前。若 index 等于表中的行數(shù),則新行將被附加到表的末尾。
如果表是空的,則新行將被插入到一個新的 <tbody> 段,該段自身會被插入表中。
拋出
若參數(shù) index 小于 0 或大于等于表中的行數(shù),該方法將拋出代碼為 INDEX_SIZE_ERR 的 DOMException 異常。
提示和注釋
提示:可以用 TableRow.insertCell() 方法給新創(chuàng)建的行添加內(nèi)容。
實例
下面的例子在表格的開頭插入一個新行:
<html>
<head>
<script type="text/javascript">
function insRow()
{
document.getElementById('myTable').insertRow(0)
}
</script>
</head>
<body>
<table id="myTable" border="1">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
</table>
<br />
<input type="button" onclick="insRow()"
value="Insert new row">
</body>
</html>