Coleção de células TableRow
Exemplo
Mostre o número de células na primeira linha:
var x = document.getElementById("myTable").rows[0].cells.length;
O resultado de x será:
2
Mais exemplos de "Experimente você mesmo" abaixo.
Definição e uso
A coleção de células retorna uma coleção de todos os elementos <td> ou <th> em uma linha da tabela.
Observação: os elementos da coleção são classificados conforme aparecem no código-fonte.
Suporte ao navegador
Collection | |||||
---|---|---|---|---|---|
cells | Yes | Yes | Yes | Yes | Yes |
Sintaxe
tableObject.cells
Propriedades
Property | Description |
---|---|
length | Returns the number of <td> and/or <th> elements in the collection. Note: This property is read-only |
Métodos
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 |
Detalhes técnicos
Versão DOM: | Objeto de documento de nível 2 principal |
---|---|
Valor de retorno: | Um objeto HTMLCollection, representando todos os elementos <td> e/ou <th> no elemento <tr>. Os elementos da coleção são classificados conforme aparecem no código-fonte |
Mais exemplos
Exemplo
[ índice ]
Alerte o innerHTML da primeira célula na primeira linha da tabela:
alert(document.getElementById("myTable").rows[0].cells[0].innerHTML);
Exemplo
item ( índice )
Alerte o innerHTML da primeira célula na primeira linha da tabela:
alert(document.getElementById("myTable").rows[0].cells.item(0).innerHTML);
Exemplo
nomeadoItem( id )
Alerte o innerHTML da célula com id="myTd" na primeira linha da tabela:
alert(document.getElementById("myTable").rows[0].cells.namedItem("myTd").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";
❮ Objeto TableRow