Table cellsCollection

Table 对象参考手册Table Object

Definition and Usage

The cells collection returns all <td> or <th> elements in a table.

Syntax

tableObject.cells

Properties

Property Description
length Returns the number of <td> or <th> elements in the collection.

Methods

Method Description
[name_or_index] An integer that specifies the position of the element to retrieve (starts at 0).
item(name_or_index) Returns the element at the specified index in the collection.
namedItem(name) Returns the element in the collection with the specified name or index.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the cells collection.


Example

Example

The following example uses cells to display the content of the first cell:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Tutorial (example.com)</title>
<script>
function displayResult(){
    alert(document.getElementById("myTable").rows[0].cells.length);
}
</script>
</head>
<body>

<table id="myTable" border="1">
    <tr>
        <td>cell 1</td>
        <td>cell 2</td>
    </tr>
    <tr>
        <td>cell 3</td>
        <td>cell 4</td>
    </tr>
</table>
<br>
<button type="button" onclick="displayResult()">Show the number of cells in the first row</button>

</body>
</html>

Try it Yourself »


实例

More Examples


Table 对象参考手册Table Object Other Extensions