Gambar Dokumen DOM HTML
Contoh
Jumlah elemen <img> dalam dokumen:
document.images.length;
Ulangi semua elemen <img>, dan keluarkan URL (src) masing-masing:
const myImages = document.images;
let text = "";
for (let i = 0; i < myImages.length; i++) {
text += myImages[i].src + "<br>";
}
URL elemen <img> pertama adalah:
document.images[0].src;
URL elemen <img> pertama adalah:
document.images.item(0).src;
Lebih banyak contoh di bawah ini.
Definisi dan Penggunaan
Properti images
mengembalikan koleksi semua elemen <img> dalam dokumen.
Properti images
mengembalikan HTMLCollection .
Properti images
ini hanya-baca.
Catatan
Properti images
tidak mengembalikan elemen <input> dengan type="image".
Lihat juga:
Koleksi HTML
Sebuah HTMLCollection adalah kumpulan node HTML.
Node dalam koleksi dapat diakses dengan nomor indeks. Indeks dimulai dari 0.
Properti panjang mengembalikan jumlah elemen dalam koleksi.
Sintaksis
document.images
Properti
Property | Description |
length | The number of <img> elements in the collection. |
Metode
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. |
Nilai Kembali
Jenis | Keterangan |
Obyek | Objek Koleksi HTML. Semua elemen <img> dalam dokumen. Elemen diurutkan saat muncul di dokumen. |
Lebih Banyak Contoh
URL elemen <img> dengan id="myImg" adalah:
document.images.namedItem("myImg").src;
Tambahkan batas hitam ke elemen <img> pertama:
document.images[0].style.border = "10px dotted black";
Dukungan Peramban
document.images
adalah fitur DOM Level 1 (1998).
Ini didukung penuh di semua browser:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |