DOM HTMLCollezione
L'oggetto HTMLCollection
Un oggetto HTMLCollection è un elenco di elementi HTML simile a un array.
Metodi come getElementsByTagName() restituisce una HTMLCollection.
Proprietà e metodi
Le seguenti proprietà e metodi possono essere utilizzati su un oggetto 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 |
Esempi
Esempio
Ottieni una raccolta HTML:
var x = document.getElementsByTagName("P");
// Returns a
collection of all the P elements in the document
Esempio
Scrivi il numero di elementi <p> nel documento:
var x = document.getElementsByTagName("P");
document.write(x.length);
Esempio
Scorri ogni elemento in una HTMLCollection:
x = document.getElementsByTagName("*");
l = x.length;
for (i = 0; i <
l; i++) {
document.write(x[i].tagName + "<br>");
}