DOM HTMLCollection
Объект HTMLCollection
Объект HTMLCollection представляет собой похожий на массив список элементов HTML.
Такие методы, как getElementsByTagName(), возвращают HTMLCollection.
Свойства и методы
В объекте 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 |
Примеры
Пример
Получить HTMLCollection:
var x = document.getElementsByTagName("P");
// Returns a
collection of all the P elements in the document
Пример
Запишите количество элементов <p> в документе:
var x = document.getElementsByTagName("P");
document.write(x.length);
Пример
Перебрать каждый элемент в HTMLCollection:
x = document.getElementsByTagName("*");
l = x.length;
for (i = 0; i <
l; i++) {
document.write(x[i].tagName + "<br>");
}