HTML DOM imagesCollection

Document 对象参考手册Document Object

Definition and Usage

The images collection returns an array of all images in the current document.

Syntax

document.images[].property


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the images collection


Examples

Example 1

Return the number of images in the document:

<html>
<body>

<img border="0" src="klematis.jpg.html" width="150" height="113">
<img border="0" src="klematis2.jpg.html" width="152" height="128">

<p>Number of images:
<script>
document.write(document.images.length);
</script></p>

</body>
</html>

The output result of the above example is:

Number of images: 2

Try it yourself »

Example 2

Return the first image in the document:

<html>
<body>

<img id="example1" border="0" src="klematis.jpg.html" width="150" height="113">
<img id="example2" border="0" src="klematis2.jpg.html" width="152" height="128">

<p>ID of the first image:
<script>
document.write(document.images[0].id);
</script></p>

</body>
</html>

The output result of the above example is:

ID of the first image: example1

Try it yourself »


Document 对象参考手册Document Object Other Extensions