Table cells 集合

Table 对象参考手册 Table 对象

定义和用法

cells 集合返回表格中所有 <td> 或 <th> 元素。

语法

tableObject.cells

Properties

属性 描述
length 返回集合中 <td> 或 <th> 元素的数目。

方法

方法 描述
[name_or_index] 一个整数,指定元素检索位置(从0开始)
item(name_or_index) 返回集合中的指定索引的元素
namedItem(name) 返回集合中指定名称索引的元素索引


浏览器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要浏览器都支持 cells 集合


实例

实例

下面的例子使用 cells 来显示出第一个单元格的内容:

<html>
<head>
<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 number of cells</button>

</body>
</html>

尝试一下 »


实例

更多实例

Alert the innerHTML of the first cell in the table's first row:

Alert the innerHTML of the first cell in the table's first row:

Alert the innerHTML of the cell with id="myTd" in the table's first row:

Change the content of the first table cell:


Table 对象参考手册 Table 对象