Kolekcja komórek TableRow
Przykład
Pokaż liczbę komórek w pierwszym wierszu:
var x = document.getElementById("myTable").rows[0].cells.length;
Wynikiem x będzie:
2
Więcej przykładów „Wypróbuj sam” poniżej.
Definicja i użycie
Kolekcja komórek zwraca kolekcję wszystkich elementów <td> lub <th> w wierszu tabeli.
Uwaga: elementy w kolekcji są sortowane tak, jak pojawiają się w kodzie źródłowym.
Obsługa przeglądarki
Collection | |||||
---|---|---|---|---|---|
cells | Yes | Yes | Yes | Yes | Yes |
Składnia
tableObject.cells
Nieruchomości
Property | Description |
---|---|
length | Returns the number of <td> and/or <th> elements in the collection. Note: This property is read-only |
Metody
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 |
Szczegóły techniczne
Wersja DOM: | Podstawowy obiekt dokumentu poziomu 2 |
---|---|
Wartość zwrotu: | Obiekt HTMLCollection reprezentujący wszystkie elementy <td> i/lub <th> w elemencie <tr>. Elementy w kolekcji są sortowane tak, jak pojawiają się w kodzie źródłowym |
Więcej przykładów
Przykład
[ indeks ]
Zaalarmuj innerHTML pierwszej komórki w pierwszym wierszu tabeli:
alert(document.getElementById("myTable").rows[0].cells[0].innerHTML);
Przykład
pozycja ( indeks )
Zaalarmuj innerHTML pierwszej komórki w pierwszym wierszu tabeli:
alert(document.getElementById("myTable").rows[0].cells.item(0).innerHTML);
Przykład
nazwany przedmiot ( id )
Zaalarmuj innerHTML komórki za pomocą id="myTd" w pierwszym wierszu tabeli:
alert(document.getElementById("myTable").rows[0].cells.namedItem("myTd").innerHTML);
Przykład
Zmień zawartość pierwszej komórki tabeli:
var x = document.getElementById("myTable").rows[0].cells;
x[0].innerHTML = "NEW CONTENT";
❮ Obiekt TableRow