TableRowセルコレクション
例
最初の行にセルの数を表示します。
var x = document.getElementById("myTable").rows[0].cells.length;
xの結果は次のようになります。
2
以下の「自分で試してみてください」の例をもっと見てください。
定義と使用法
セルコレクションは、テーブル行のすべての<td>または<th>要素のコレクションを返します。
注:コレクション内の要素は、ソースコードに表示されるとおりに並べ替えられます。
ブラウザのサポート
Collection | |||||
---|---|---|---|---|---|
cells | Yes | Yes | Yes | Yes | Yes |
構文
tableObject.cells
プロパティ
Property | Description |
---|---|
length | Returns the number of <td> and/or <th> elements in the collection. Note: This property is read-only |
メソッド
Method | Description |
---|---|
[index] | Returns the <td> and/or <th> element from the collection with the specified index (starts at 0). Note: Returns null if the index number is out of range |
item(index) | Returns the <td> and/or <th> element from the collection with the specified index (starts at 0). Note: Returns null if the index number is out of range |
namedItem(id) | Returns the <td> and/or <th> element from the collection with the specified id. Note: Returns null if the id does not exist |
技術的な詳細
DOMバージョン: | コアレベル2ドキュメントオブジェクト |
---|---|
戻り値: | <tr>要素内のすべての<td>および/または<th>要素を表すHTMLCollectionオブジェクト。コレクション内の要素は、ソースコードに表示されるとおりに並べ替えられます |
その他の例
例
[インデックス]
テーブルの最初の行の最初のセルのinnerHTMLに警告します。
alert(document.getElementById("myTable").rows[0].cells[0].innerHTML);
例
item(インデックス)
テーブルの最初の行の最初のセルのinnerHTMLに警告します。
alert(document.getElementById("myTable").rows[0].cells.item(0).innerHTML);
例
namedItem(id)
テーブルの最初の行にid = "myTd"を指定してセルのinnerHTMLに警告します。
alert(document.getElementById("myTable").rows[0].cells.namedItem("myTd").innerHTML);
例
最初のテーブルセルの内容を変更します。
var x = document.getElementById("myTable").rows[0].cells;
x[0].innerHTML = "NEW CONTENT";
❮TableRowオブジェクト