Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM tr cells collection

tr cells collection

tr Object Reference tr object

Example

Display the number of cells in the first row:

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

x The output result is:

2

Try it »


Definition and Usage

The cells collection returns the number of all or elements in a table row.

Note: The order of elements in the collection is consistent with their order in the source code.


Browser support

Union
cells Yes Yes Yes Yes Yes

Syntax



tableObject.cells


property

property Description
length Returns the number of in the collection or Number of elements.
 **注意:** 这个属性是只读的。 |


Method

Method Description
[name_or_index] An integer specifying the element retrieval position (starting from 0)
item(name_or_index) Returns the element at the specified index in the collection
namedItem(name) Returns the index of the element specified by name index in the collection

Technical description

all in
DOM version: Core Level 2 Document Object
Return value: HTMLCollection object, representing
or elements. The order of elements in the collection is consistent with their order in the source code.

More examples

Example

[index]

Pop up the content of the first cell in the first row:

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

Try it »

Example

item(index)

Pop up the content of the first cell in the first row:

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

Try it »

Example

namedItem(id)

Pop up the content of the cell with id="myTd" in the first row:

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

Try it »

Example

Modify the content of the first cell:

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

Try it »


tr Object Reference tr object