tr cells 集合
實例
顯示第一行的單元格數量:
var x = document.getElementById("myTable").rows[0].cells.length;
x 輸出結果為:
2
定義和用法
cells 集合返回表格行中所有 <td> 或 <th> 元素的個數。
注意: 元素在集合中的順序為與在源代碼中的順序是一致的。
流覽器支持
集合 | |||||
---|---|---|---|---|---|
cells | Yes | Yes | Yes | Yes | Yes |
語法
tableObject.cells
屬性
屬性 | 描述 |
---|---|
length | 返回集合中 <td> 或 <th> 元素的個數。 注意: 這個屬性是只讀的。 |
方法
方法 | 描述 |
---|---|
[name_or_index] | 一個整數,指定元素檢索位置(從0開始) |
item(name_or_index) | 返回集合中的指定索引的元素 |
namedItem(name) | 返回集合中指定名稱索引的元素索引 |
技術描述
DOM 版本: | Core Level 2 Document Object |
---|---|
返回值: | HTMLCollection 對象, 表示 <tr> 中所有 <td> 或 <th> 元素。 元素在集合中的順序為與在源代碼中的順序是一致的。 |
更多實例
實例
[index]
彈出第一行第一個單元格的內容:
alert(document.getElementById("myTable").rows[0].cells[0].innerHTML);
實例
item(index)
彈出第一行第一個單元格的內容:
alert(document.getElementById("myTable").rows[0].cells.item(0).innerHTML);
實例
namedItem(id)
彈出第一行 id="myTd" 單元格的內容:
alert(document.getElementById("myTable").rows[0].cells.namedItem("myTd").innerHTML);
實例
修改第一個單元格的內容:
var x = document.getElementById("myTable").rows[0].cells;
x[0].innerHTML = "NEW CONTENT";
x[0].innerHTML = "NEW CONTENT";