TableRow cells collection

Definition and usage

cells The collection returns all <td> Or <th> Element collection.

Tambayi:集合中的元素按照它们在源代码中出现的顺序进行排序。

Instance

Example 1

Show the number of cells in the first row:

var x = document.getElementById("myTable").rows[0].cells.length;

Try it yourself

The result of x will be:

2

Example 2: [index]

Output the innerHTML of the first cell in the first row of the table:

alert(document.getElementById("myTable").rows[0].cells[0].innerHTML);

Try it yourself

Example 3: item(index)

Output the innerHTML of the first cell in the first row of the table:

alert(document.getElementById("myTable").rows[0].cells.item(0).innerHTML);

Try it yourself

Example 4: namedItem(id)

Output the innerHTML of the cell with id="myTd" in the first row of the table:

alert(document.getElementById("myTable").rows[0].cells.namedItem("myTd").innerHTML);

Try it yourself

Example 5

Change the content of the first table cell:

var x = document.getElementById("myTable").rows[0].cells;
x[0].innerHTML = "NEW CONTENT";

Try it yourself

Syntax

tableObject.cells

Attribute

Attribute Miyar
length

Return the number of <td> and/or <th> elements in the collection.

Tambayi:This property is read-only.

Hodari

Hodari Miyar
[index]

Return the <td> and/or <th> element with the specified index in the collection (starting from 0).

Tambayi:If the index number is out of range, return null.

item(index)

Return the <td> and/or <th> element with the specified index in the collection (starting from 0).

Tambayi:If the index number is out of range, return null.

namedItem(id)

Yan ba <td> da kai <th> element dake id za kai ying yong jing li.

Tambayi:Id ba da bai, kai bai null.

技术细节

元素中的所有
DOM 版本: Core Level 2 Document Object
返回值:

HTMLCollection 对象,表示

和/或 元素。

集合中的元素按照它们在源代码中出现的顺序进行排序。

浏览器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持