HTML DOM Document images 屬性
- 上一頁 head
- 下一頁 implementation
- 返回上一層 HTML DOM Documents
定義和用法
images
屬性返回文檔中所有 <img> 元素的集合。
images
屬性返回 HTMLCollection。
image
s 屬性是只讀的。
提示:images 屬性不返回 type="image" 的 <input> 元素。
實例
例子 1
文檔中 <img> 元素的數量:
document.images.length;
例子 2
遍歷所有 <img> 元素,并輸出每個元素的 URL (src):
const myImages = document.images; let text = ""; for (let i = 0; i < myImages.length; i++) { text += myImages[i].src + "<br>"; }
例子 3
第一個 <img> 元素的 URL 是:
document.images[0].src;
例子 4
第一個 <img> 元素的 URL 是:
document.images.item(0).src;
例子 5
id="myImg" 的 <img> 元素的 URL 是:
document.images.namedItem("myImg").src;
例子 6
為第一個 <img> 元素添加黑色邊框:
document.images[0].style.border = "10px dotted black";
語法
document.images
屬性
屬性 | 描述 |
---|---|
length | 集合中 <img> 元素的數量。 |
方法
方法 | 描述 |
---|---|
[index] |
返回有指定索引的元素(從 0 開始)。 如果索引超出范圍,則返回 null。 |
item(index) |
返回有指定索引的元素(從 0 開始)。 如果索引超出范圍,則返回 null。 |
namedItem(id) |
返回有指定 id 的元素。 如果 id 不存在,則返回 null。 |
返回值
類型 | 描述 |
---|---|
對象 |
HTMLCollection 對象。 文檔中的所有 <img> 元素。 按照它們在源代碼中出現的順序進行排序。 |
瀏覽器支持
document.images
是 DOM Level 1 (1998) 特性。
所有瀏覽器都支持它:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
支持 | 9-11 | 支持 | 支持 | 支持 | 支持 |
- 上一頁 head
- 下一頁 implementation
- 返回上一層 HTML DOM Documents