Table rows 集合

Table 对象参考手册 Table 对象

定义和用法

rows 集合返回表格中所有行(TableRow 对象)的一个数组,即一个 HTMLCollection。

该集合包括 <thead>、<tfoot> 和 <tbody> 中定义的所有行。

语法

tableObject.rows

属性

属性 描述
length 返回集合中 <tr> 元素的数目

方法

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


浏览器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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


实例

实例

显示表格中行的数目:

<html>
<head>
<script>
function displayResult()
{
alert(document.getElementById("myTable").rows.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 rows in table</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 对象