Proprietà rows
La proprietà rows memorizza una collezione di righe
tr.
Può essere applicata sia alla tabella che alle
sue sezioni tHead,
tBodies,
tFoot.
Sintassi
tabella.rows;
Esempio
Iteriamo tutte le righe della tabella:
<table id="table">
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
<tr>
<td>4</td><td>5</td><td>6</td>
</tr>
<tr>
<td>7</td><td>8</td><td>9</td>
</tr>
</table>
let table = document.querySelector('#table');
for (let row of table.rows) {
console.log(row);
}
Esempio
Iteriamo tutte le righe della tabella utilizzando
la proprietà rows, e in ogni riga iteriamo
le sue celle utilizzando la proprietà cells:
<table id="table">
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
<tr>
<td>4</td><td>5</td><td>6</td>
</tr>
<tr>
<td>7</td><td>8</td><td>9</td>
</tr>
</table>
let table = document.querySelector('#table');
for (let row of table.rows) {
for (let cell of row.cells) {
console.log(cell);
}
}
Esempio
Scopriamo il numero di righe della tabella:
<table id="table">
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
<tr>
<td>4</td><td>5</td><td>6</td>
</tr>
<tr>
<td>7</td><td>8</td><td>9</td>
</tr>
</table>
let table = document.querySelector('#table');
console.log(table.rows.length);
Risultato dell'esecuzione del codice:
3