tr cells collection
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
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
| 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