Bộ sưu tập ô TableRow
Thí dụ
Hiển thị số ô trong hàng đầu tiên:
var x = document.getElementById("myTable").rows[0].cells.length;
Kết quả của x sẽ là:
2
Thêm các ví dụ "Hãy tự mình thử" bên dưới.
Định nghĩa và Cách sử dụng
Tập hợp ô trả về tập hợp tất cả các phần tử <td> hoặc <th> trong một hàng bảng.
Lưu ý: Các phần tử trong bộ sưu tập được sắp xếp như chúng xuất hiện trong mã nguồn.
Hỗ trợ trình duyệt
Collection | |||||
---|---|---|---|---|---|
cells | Yes | Yes | Yes | Yes | Yes |
Cú pháp
tableObject.cells
Tính chất
Property | Description |
---|---|
length | Returns the number of <td> and/or <th> elements in the collection. Note: This property is read-only |
Phương pháp
Method | Description |
---|---|
[index] | Returns the <td> and/or <th> element from the collection with the specified index (starts at 0). Note: Returns null if the index number is out of range |
item(index) | Returns the <td> and/or <th> element from the collection with the specified index (starts at 0). Note: Returns null if the index number is out of range |
namedItem(id) | Returns the <td> and/or <th> element from the collection with the specified id. Note: Returns null if the id does not exist |
Chi tiết kỹ thuật
Phiên bản DOM: | Đối tượng tài liệu cấp 2 cốt lõi |
---|---|
Giá trị trả lại: | Đối tượng HTMLCollection, đại diện cho tất cả các phần tử <td> và / hoặc <th> trong phần tử <tr>. Các phần tử trong bộ sưu tập được sắp xếp khi chúng xuất hiện trong mã nguồn |
Các ví dụ khác
Thí dụ
[ chỉ mục ]
Cảnh báo InternalHTML của ô đầu tiên trong hàng đầu tiên của bảng:
alert(document.getElementById("myTable").rows[0].cells[0].innerHTML);
Thí dụ
item ( chỉ mục )
Cảnh báo InternalHTML của ô đầu tiên trong hàng đầu tiên của bảng:
alert(document.getElementById("myTable").rows[0].cells.item(0).innerHTML);
Thí dụ
NameItem ( id )
Cảnh báo InternalHTML của ô có id = "myTd" ở hàng đầu tiên của bảng:
alert(document.getElementById("myTable").rows[0].cells.namedItem("myTd").innerHTML);
Thí dụ
Thay đổi nội dung của ô đầu tiên trong bảng:
var x = document.getElementById("myTable").rows[0].cells;
x[0].innerHTML = "NEW CONTENT";
❮ Đối tượng TableRow