Obrázky dokumentů HTML DOM
Příklad
Počet prvků <img> v dokumentu:
document.images.length;
Projděte všechny prvky <img> a zadejte adresu URL (src) každého:
const myImages = document.images;
let text = "";
for (let i = 0; i < myImages.length; i++) {
text += myImages[i].src + "<br>";
}
Adresa URL prvního prvku <img> je:
document.images[0].src;
Adresa URL prvního prvku <img> je:
document.images.item(0).src;
Další příklady níže.
Definice a použití
Vlastnost images
vrací kolekci všech prvků <img> v dokumentu.
Vlastnost images
vrací HTMLCollection .
Vlastnost images
je pouze pro čtení.
Poznámka
Vlastnost images
nevrací prvky <input> s type="image".
Viz také:
HTML kolekce
HTMLCollection je kolekce uzlů HTML.
K uzlům v kolekci lze přistupovat pomocí čísel indexů. Index začíná na 0.
Vlastnost length vrátí počet prvků v kolekci.
Syntax
document.images
Vlastnosti
Property | Description |
length | The number of <img> elements in the collection. |
Metody
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. |
Návratová hodnota
Typ | Popis |
Objekt | Objekt HTMLCollection. Všechny prvky <img> v dokumentu. Prvky jsou seřazeny tak, jak se objevují v dokumentu. |
Další příklady
Adresa URL prvku <img> s id="myImg" je:
document.images.namedItem("myImg").src;
Přidejte černé ohraničení k prvnímu prvku <img>:
document.images[0].style.border = "10px dotted black";
Podpora prohlížeče
document.images
je funkce DOM úrovně 1 (1998).
Je plně podporován ve všech prohlížečích:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |