डोम एचटीएमएलसंग्रह
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 |
उदाहरण
उदाहरण
एक 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);
उदाहरण
HTMLCollection में प्रत्येक तत्व के माध्यम से लूप करें:
x = document.getElementsByTagName("*");
l = x.length;
for (i = 0; i <
l; i++) {
document.write(x[i].tagName + "<br>");
}