DOM HTMLCollection
Objekt HTMLCollection
Objekt HTMLCollection je seznam prvků HTML podobný poli.
Metody jako getElementsByTagName() vrací HTMLCollection.
Vlastnosti a metody
Na objektu HTMLCollection lze použít následující vlastnosti a metody:
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 |
Příklady
Příklad
Získejte HTML Collection:
var x = document.getElementsByTagName("P");
// Returns a
collection of all the P elements in the document
Příklad
Napište počet prvků <p> v dokumentu:
var x = document.getElementsByTagName("P");
document.write(x.length);
Příklad
Projděte každý prvek v HTMLCollection:
x = document.getElementsByTagName("*");
l = x.length;
for (i = 0; i <
l; i++) {
document.write(x[i].tagName + "<br>");
}