Coleção de linhas da tabela
Exemplo
Descubra quantas linhas existem em uma tabela:
var x = document.getElementById("myTable").rows.length;
O resultado de x será:
2
Mais exemplos de "Experimente você mesmo" abaixo.
Definição e uso
A coleção de linhas retorna uma coleção de todos os elementos <tr> em uma tabela.
Observação: os elementos da coleção são classificados conforme aparecem no código-fonte.
Dica: Use o método insertRow() para criar uma nova linha (<tr>).
Dica: Use o método deleteRow() para remover uma linha.
Dica: Use o método insertCell() para criar uma nova célula (<td>).
Dica: Use o método deleteCell() para excluir uma célula.
Dica: Use a coleção de células para retornar uma coleção de todos os elementos <td> ou <th> em uma tabela.
Suporte ao navegador
Collection | |||||
---|---|---|---|---|---|
rows | Yes | Yes | Yes | Yes | Yes |
Sintaxe
tableObject.rows
Propriedades
Property | Description |
---|---|
length | Returns the number of <tr> elements in the collection. Note: This property is read-only |
Métodos
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 |
Detalhes técnicos
Versão DOM: | Objeto de documento de nível 2 principal |
---|---|
Valor de retorno: | Um objeto HTMLCollection, representando todos os elementos <tr> no elemento <table>. Os elementos da coleção são classificados conforme aparecem no código-fonte |
Mais exemplos
Exemplo
[ índice ]
Alerte o innerHTML do primeiro elemento <tr> (índice 0) na tabela:
alert(document.getElementById("myTable").rows[0].innerHTML;
Exemplo
item ( índice )
Alerte o innerHTML do primeiro elemento <tr> (índice 0) na tabela:
alert(document.getElementById("myTable").rows.item(0).innerHTML);
Exemplo
nomeadoItem( id )
Alerte o innerHTML do elemento <tr> com id="myRow" na tabela:
alert(document.getElementById("myTable").rows.namedItem("myRow").innerHTML);
Exemplo
Altere o conteúdo da primeira célula da tabela:
var x = document.getElementById("myTable").rows[0].cells;
x[0].innerHTML = "NEW CONTENT";
Páginas relacionadas
Referência HTML: tag HTML <tr>
Referência JavaScript: objeto HTML DOM TableRow
❮ Objeto de Tabela