Colección DOM HTML
El objeto HTMLCollection
Un objeto HTMLCollection es una lista similar a una matriz de elementos HTML.
Métodos como getElementsByTagName() devuelven HTMLCollection.
Propiedades y Métodos
Las siguientes propiedades y métodos se pueden usar en un objeto HTMLCollection:
Property / Method | Description |
---|---|
item() | Returns the element at the specified index in an HTMLCollection |
length | Returns the number of elements in an HTMLCollection |
namedItem() | Returns the element with the specified ID, or name, in an HTMLCollection |
Ejemplos
Ejemplo
Obtenga una colección HTML:
var x = document.getElementsByTagName("P");
// Returns a
collection of all the P elements in the document
Ejemplo
Escriba el número de elementos <p> en el documento:
var x = document.getElementsByTagName("P");
document.write(x.length);
Ejemplo
Recorre todos los elementos de una HTMLCollection:
x = document.getElementsByTagName("*");
l = x.length;
for (i = 0; i <
l; i++) {
document.write(x[i].tagName + "<br>");
}