DOM HTMLCollection
كائن مجموعة HTML
كائن 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 |
أمثلة
مثال
احصل على مجموعة HTML:
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);
مثال
تكرار كل عنصر في مجموعة HTML:
x = document.getElementsByTagName("*");
l = x.length;
for (i = 0; i <
l; i++) {
document.write(x[i].tagName + "<br>");
}