-
insertCell()
鎖定
insertCell() 方法用於在 HTML 表的一行的指定位置插入一個空的 元素。
- 中文名
- insertCell()
- 拋 出
- 參數 index 小於 0
- 語 法
- index
insertCell()簡介
insertCell()語法
tablerowObject.insertCell(index)返回值
insertCell()説明
該方法將創建一個新的 <td> 元素,把它插入行中指定的位置。新單元格將被插入當前位於 index 指定位置的表元之前。如果 index 等於行中的單元格數,則新單元格被附加在行的末尾。
請注意,該方法只能插入 <td> 數據表元。若需要給行添加頭表元,必須用 Document.createElement() 方法和 Node.insertBefore() 方法(或相關的方法)創建並插入一個 <th> 元素。
insertCell()拋出
若參數 index 小於 0 或大於等於行中的的表元數,該方法將拋出代碼為 INDEX_SIZE_ERR 的 DOMException 異常。
insertCell()實例
insertCell()實例一
下面的例子在表格行中插入了一個單元格:
<html>
<head>
<script type=“text/javascript”>
function insCell()
{
var x=document.getElementById("tr2").insertCell(0);
x.innerHTML=“John”;
}
</script>
</head>
<body>
<table border=“1”>
<tr id=“tr1”>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr id=“tr2”>
<td>Peter</td>
<td>Griffin</td>
</tr>
</table>
<br />
<input type=“button” onclick="insCell()" value=“Insert cell”>
</body>
insertCell()實例二
在表格中插入一個新列。
<table id=“mytable” width=“80%” border=“1” cellpadding=“0” cellspacing=“0” bordercolor=“blue”>
<tr><td>1</td><td>2</td></tr>
<tr><td>3</td><td>4</td></tr>
</table>
<button onclick=“InsertCol()">插入列</button>
<script type=“text/javascript”>
function InsertCol()
{
var n = document.getElementById(“mytable”)。rows.length;
for( i=0; i<n; i++ )
{
x = document.getElementById(“mytable”)。rows[i].insertCell(0);
x.innerHTML = “ ”;
}
}
</script>
注:如果沒有合併單元格,應保證表格每行中的單元格數量相同,否則生成的表格會變形。
[2]
- 參考資料
-
- 1. 簡介、實例一資料來源
- 2. 實例二資料來源
- 詞條統計
-
- 瀏覽次數:次
- 編輯次數:13次歷史版本
- 最近更新: LYPLGFX