Kolekcja wierszy tabeli
Przykład
Dowiedz się, ile wierszy jest w tabeli:
var x = document.getElementById("myTable").rows.length;
Wynikiem x będzie:
2
Więcej przykładów „Wypróbuj sam” poniżej.
Definicja i użycie
Kolekcja rows zwraca kolekcję wszystkich elementów <tr> w tabeli.
Uwaga: elementy w kolekcji są sortowane tak, jak pojawiają się w kodzie źródłowym.
Wskazówka: Użyj metody insertRow() , aby utworzyć nowy wiersz (<tr>).
Porada: Użyj metody deleteRow() , aby usunąć wiersz.
Wskazówka: Użyj metody insertCell() , aby utworzyć nową komórkę (<td>).
Porada: Użyj metody deleteCell() , aby usunąć komórkę.
Wskazówka: Użyj kolekcji komórek, aby zwrócić kolekcję wszystkich elementów <td> lub <th> w tabeli.
Obsługa przeglądarki
Collection | |||||
---|---|---|---|---|---|
rows | Yes | Yes | Yes | Yes | Yes |
Składnia
tableObject.rows
Nieruchomości
Property | Description |
---|---|
length | Returns the number of <tr> elements in the collection. Note: This property is read-only |
Metody
Method | Description |
---|---|
[index] | Returns the <tr> 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 <tr> 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 <tr> 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 <tr> w elemencie <table>. Elementy w kolekcji są sortowane tak, jak pojawiają się w kodzie źródłowym |
Więcej przykładów
Przykład
[ indeks ]
Zaalarmuj innerHTML pierwszego elementu <tr> (indeks 0) w tabeli:
alert(document.getElementById("myTable").rows[0].innerHTML;
Przykład
pozycja ( indeks )
Zaalarmuj innerHTML pierwszego elementu <tr> (indeks 0) w tabeli:
alert(document.getElementById("myTable").rows.item(0).innerHTML);
Przykład
nazwany przedmiot ( id )
Zaalarmuj innerHTML elementu <tr> za pomocą id="myRow" w tabeli:
alert(document.getElementById("myTable").rows.namedItem("myRow").innerHTML);
Przykład
Zmień zawartość pierwszej komórki tabeli:
var x = document.getElementById("myTable").rows[0].cells;
x[0].innerHTML = "NEW CONTENT";
Powiązane strony
Odniesienie HTML: tag HTML <tr>
Odniesienie do JavaScript: HTML DOM TableRow Object
❮ Obiekt tabeli