Kolekcja DOM HTML
Obiekt HTMLCollection
Obiekt HTMLCollection to podobna do tablicy lista elementów HTML.
Metody takie jak getElementsByTagName() zwracają HTMLCollection.
Właściwości i metody
W obiekcie HTMLCollection można używać następujących właściwości i metod:
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 |
Przykłady
Przykład
Pobierz kolekcję HTML:
var x = document.getElementsByTagName("P");
// Returns a
collection of all the P elements in the document
Przykład
Wpisz liczbę <p> elementów w dokumencie:
var x = document.getElementsByTagName("P");
document.write(x.length);
Przykład
Przejdź przez każdy element w HTMLCollection:
x = document.getElementsByTagName("*");
l = x.length;
for (i = 0; i <
l; i++) {
document.write(x[i].tagName + "<br>");
}