HTMLDOMドキュメント画像
例
ドキュメント内の<img>要素の数:
document.images.length;
すべての<img>要素をループし、それぞれのURL(src)を出力します。
const myImages = document.images;
let text = "";
for (let i = 0; i < myImages.length; i++) {
text += myImages[i].src + "<br>";
}
最初の<img>要素のURLは次のとおりです。
document.images[0].src;
最初の<img>要素のURLは次のとおりです。
document.images.item(0).src;
以下のその他の例。
定義と使用法
このimages
プロパティは、ドキュメント内のすべての<img>要素のコレクションを返します。
images
プロパティはHTMLCollectionを返します。
images
プロパティは読み取り専用です。
HTMLCollection
HTMLCollectionは、HTMLノードのコレクションです。
コレクション内のノードには、インデックス番号でアクセスできます。インデックスは0から始まります。
lengthプロパティは、コレクション内の要素の数を返します。
構文
document.images
プロパティ
Property | Description |
length | The number of <img> elements in the collection. |
メソッド
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. |
戻り値
タイプ | 説明 |
物体 | HTMLCollectionオブジェクト。 ドキュメント内のすべての<img>要素。 要素は、ドキュメントに表示されるとおりに並べ替えられます。 |
その他の例
id = "myImg"の<img>要素のURLは次のとおりです。
document.images.namedItem("myImg").src;
最初の<img>要素に黒い境界線を追加します。
document.images[0].style.border = "10px dotted black";
ブラウザのサポート
document.images
DOMレベル1(1998)の機能です。
すべてのブラウザで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |