Colección de filas de tabla
Ejemplo
Averigüe cuántas filas hay en una tabla:
var x = document.getElementById("myTable").rows.length;
El resultado de x será:
2
Más ejemplos de "Pruébelo usted mismo" a continuación.
Definición y uso
La colección de filas devuelve una colección de todos los elementos <tr> de una tabla.
Nota: Los elementos de la colección se ordenan tal como aparecen en el código fuente.
Sugerencia: use el método insertRow() para crear una nueva fila (<tr>).
Sugerencia: utilice el método deleteRow() para eliminar una fila.
Sugerencia: use el método insertCell() para crear una nueva celda (<td>).
Sugerencia: utilice el método deleteCell() para eliminar una celda.
Sugerencia: use la colección de celdas para devolver una colección de todos los elementos <td> o <th> en una tabla.
Compatibilidad con navegador
Collection | |||||
---|---|---|---|---|---|
rows | Yes | Yes | Yes | Yes | Yes |
Sintaxis
tableObject.rows
Propiedades
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 |
Detalles técnicos
Versión DOM: | Objeto de documento básico de nivel 2 |
---|---|
Valor devuelto: | Un objeto HTMLCollection, que representa todos los elementos <tr> en el elemento <table>. Los elementos de la colección se ordenan tal como aparecen en el código fuente. |
Más ejemplos
Ejemplo
[ índice ]
Alerta al HTML interno del primer elemento <tr> (índice 0) en la tabla:
alert(document.getElementById("myTable").rows[0].innerHTML;
Ejemplo
artículo ( índice )
Alerta al HTML interno del primer elemento <tr> (índice 0) en la tabla:
alert(document.getElementById("myTable").rows.item(0).innerHTML);
Ejemplo
elemento con nombre ( id )
Alerta al HTML interno del elemento <tr> con id="myRow" en la tabla:
alert(document.getElementById("myTable").rows.namedItem("myRow").innerHTML);
Ejemplo
Cambia el contenido de la primera celda de la tabla:
var x = document.getElementById("myTable").rows[0].cells;
x[0].innerHTML = "NEW CONTENT";
páginas relacionadas
Referencia HTML: etiqueta HTML <tr>
Referencia de JavaScript: objeto HTML DOM TableRow
❮ Objeto de tabla