HTML DOM Documentafbeeldingen
Voorbeeld
Het aantal <img> elementen in het document:
document.images.length;
Loop over alle <img>-elementen en voer de URL (src) van elk uit:
const myImages = document.images;
let text = "";
for (let i = 0; i < myImages.length; i++) {
text += myImages[i].src + "<br>";
}
De URL van het eerste <img> element is:
document.images[0].src;
De URL van het eerste <img> element is:
document.images.item(0).src;
Hieronder meer voorbeelden.
Definitie en gebruik
De images
eigenschap retourneert een verzameling van alle <img>-elementen in een document.
De images
eigenschap retourneert een HTMLCollection .
De images
eigenschap is alleen-lezen.
Opmerking
De images
eigenschap retourneert geen <input>-elementen met type="image".
Zie ook:
HTMLCollection
Een HTMLCollection is een verzameling HTML-knooppunten.
Knooppunten in een verzameling zijn toegankelijk via indexnummers. De index begint bij 0.
De eigenschap length retourneert het aantal elementen in de verzameling.
Syntaxis
document.images
Eigenschappen
Property | Description |
length | The number of <img> elements in the collection. |
Methoden:
Method | Description |
[index] | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
item(index) | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
namedItem(id) | Returns the element with the specified id. Returns null if the id does not exist. |
Winstwaarde
Type | Beschrijving |
Object | Een HTMLCollection-object. Alle <img> elementen in het document. De elementen worden gesorteerd zoals ze in het document worden weergegeven. |
Meer voorbeelden
De URL van het <img> element met id="myImg" is:
document.images.namedItem("myImg").src;
Voeg een zwarte rand toe aan het eerste <img> element:
document.images[0].style.border = "10px dotted black";
Browserondersteuning
document.images
is een DOM Level 1 (1998)-functie.
Het wordt volledig ondersteund in alle browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |