HTML DOM 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" ;
嘗試一下»