XML DOM HTMLCollection Object
- Vorige pagina DOM Event
- Volgende pagina DOM HTMLDocument
HTMLCollection object
HTMLCollection is an interface representing a collection of HTML elements, providing methods and properties that can be traversed in the list.
The HTMLCollection in the HTML DOM is 'live'; if the basic document changes, those changes are immediately displayed through all HTMLCollection objects.
Each item below (and the properties they specify) returns an HTMLCollection:
- Document (images, applets, links, forms, anchors)
- form (elements)
- map (areas)
- select (options)
- table (rows, tBodies)
- tableSection (rows)
- row (cells)
Many properties of the HTMLDocument interface are HTMLCollection objects, providing a convenient way to access document elements such as forms, images, and links.form.elements and select.options are all HTMLCollection objects. HTMLCollection also provides Table rows as well as TableRow a convenient method for the various cells.
As mentioned above, the HTMLCollection object is a collection of HTML elements with methods, which can be used to obtain elements by their position in the document or their id and name attributes. In JavaScript, the behavior of the HTMLCollection object is like a read-only array, and you can use JavaScript's bracket notation to index an HTMLCollection object by number or name without calling item() methodand namedItem() method.
The HTMLCollection object is read-only and cannot have new elements added to it, even if the JavaScript array syntax is used.
The HTMLCollection object and NodeList objectVery similar, but the former may be indexed by both name and number.
Properties of the HTMLCollection object
Properties | Beschrijving |
---|---|
cssRules | Read-only properties, return an integer indicating the list length (i.e., the number of elements in the collection). |
HTMLCollection object methods
Methode | Beschrijving |
---|---|
item() | Retourneert het element (knopen) op de opgegeven positie in de verzameling. |
namedItem() | Retourneert het element (knopen) in de verzameling met de naam-eigenschap of id-eigenschap die de opgegeven waarde heeft. |
Voorbeeld
var c = document.forms; //Dit is een HTMLCollection object van het form-element var firstform = c[0]; //Kan worden gebruikt als een numerieke array var lastform = c[c.length-1]; //De length-eigenschap retourneert het aantal elementen var address = c["address"]; //Kan worden gebruikt als een verbonden array var address = c.address; //JavaScript staat dergelijke notatie toe
Gerelateerde pagina's
XML DOM referentiemanual:HTMLDocument object
XML DOM referentiemanual:NodeList object
Referentiemanual:HTML DOM referentiemanual
- Vorige pagina DOM Event
- Volgende pagina DOM HTMLDocument