Koleksi baris tabel
Contoh
Cari tahu berapa banyak baris yang ada dalam sebuah tabel:
var x = document.getElementById("myTable").rows.length;
Hasil dari x menjadi:
2
Lebih banyak contoh "Coba Sendiri" di bawah ini.
Definisi dan Penggunaan
Koleksi baris mengembalikan koleksi semua elemen <tr> dalam tabel.
Catatan: Elemen dalam koleksi diurutkan seperti yang muncul di kode sumber.
Tip: Gunakan metode insertRow() untuk membuat baris baru (<tr>).
Tip: Gunakan metode deleteRow() untuk menghapus baris.
Tip: Gunakan metode insertCell() untuk membuat sel baru (<td>).
Tip: Gunakan metode deleteCell() untuk menghapus sel.
Tips: Gunakan koleksi sel untuk mengembalikan koleksi semua elemen <td> atau <th> dalam tabel.
Dukungan Peramban
Collection | |||||
---|---|---|---|---|---|
rows | Yes | Yes | Yes | Yes | Yes |
Sintaksis
tableObject.rows
Properti
Property | Description |
---|---|
length | Returns the number of <tr> elements in the collection. Note: This property is read-only |
Metode
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 |
Detail Teknis
Versi DOM: | Objek Dokumen Tingkat 2 Inti |
---|---|
Nilai Kembali: | Objek HTMLCollection, mewakili semua elemen <tr> dalam elemen <table>. Elemen-elemen dalam koleksi diurutkan seperti yang muncul dalam kode sumber |
Lebih Banyak Contoh
Contoh
[ indeks ]
Beri tahu innerHTML dari elemen <tr> pertama (indeks 0) dalam tabel:
alert(document.getElementById("myTable").rows[0].innerHTML;
Contoh
barang ( indeks )
Beri tahu innerHTML dari elemen <tr> pertama (indeks 0) dalam tabel:
alert(document.getElementById("myTable").rows.item(0).innerHTML);
Contoh
bernamaItem( id )
Beri tahu innerHTML dari elemen <tr> dengan id="myRow" di tabel:
alert(document.getElementById("myTable").rows.namedItem("myRow").innerHTML);
Contoh
Ubah konten sel tabel pertama:
var x = document.getElementById("myTable").rows[0].cells;
x[0].innerHTML = "NEW CONTENT";
Halaman Terkait
Referensi HTML: tag HTML <tr>
Referensi JavaScript: Objek TableRow HTML DOM
Objek Tabel