集合 Table rows

定义和用法

rows 集合返回表中所有 tr 元素的集合

Note:集合中的元素按照它们在源代码中出现的顺序进行排序。

Tip:Use 方法 insertRow() 创建新行 (tr)

Tip:Use 方法 deleteRow() Delete row.

Tip:Use insertCell() method Create a new cell (<td>).

Tip:Use deleteCell() method Delete cell.

Tip:Use cells collection Returns a collection of all <td> or <th> elements in the table.

See also:

HTML Reference Manual:HTML <tr> ɗanararɗe

JavaScript Reference Manual:HTML DOM TableRow ɗanararɗe

Instance

Example 1

Find out how many rows are in the table:

var x = document.getElementById("myTable").rows.length;

Try It Yourself

The result of x will be:

2

Example 2: [index]

Prompt the innerHTML of the first <tr> element (index 0) in the table:

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

Try It Yourself

Example 3: item(index)

Prompt the innerHTML of the first <tr> element (index 0) in the table:

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

Try It Yourself

Example 4: namedItem(id)

Prompt the innerHTML of the <tr> element with id="myRow" in the table:

alert(document.getElementById("myTable").rows.namedItem("myRow").innerHTML);

Try It Yourself

Example 5

Change the content of the first table cell:

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

Try It Yourself

Syntax

tableObject.rows

Property

Property Description
length

Returns the number of <tr> elements in the collection.

Note:This property is read-only.

Method

Method Description
[index]

Returns the <tr> element with the specified index in the collection (starting from 0).

Note:If the index number is out of range, returns null.

item(index)

Returns the <tr> element with the specified index in the collection (starting from 0).

Note:If the index number is out of range, returns null.

namedItem(id)

Returns the <tr> element with the specified id in the collection.

Note:If id does not exist, returns null.

Technical Details

DOM Version: Core Level 2 Document Object
Return Value:

HTMLCollection naɗi, wanda ke wakilci <tr> na <table> na hanyar.

集合中的元素按照它们在源代码中出现的顺序进行排序。

浏览器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持