HTML DOM images 集合

定義和用法
images 集合返回當前文檔中所有圖片的數組。
語法
document.images[].property
流覽器支持
所有主要流覽器都支持 images 集合
實例
實例 1
返回文檔的圖片數:
<html>
<body>
<img border="0" src="klematis.jpg" width="150" height="113">
<img border="0" src="klematis2.jpg" width="152" height="128">
<p>圖像數目:
<script>
document.write(document.images.length);
</script></p>
</body>
</html>
<body>
<img border="0" src="klematis.jpg" width="150" height="113">
<img border="0" src="klematis2.jpg" width="152" height="128">
<p>圖像數目:
<script>
document.write(document.images.length);
</script></p>
</body>
</html>
以上實例輸出結果:
圖像數目: 2
實例 2
返回文檔中的第一張圖片:
<html>
<body>
<img id="zaixian1" border="0" src="klematis.jpg" width="150" height="113">
<img id="zaixian2" border="0" src="klematis2.jpg" width="152" height="128">
<p>第一個圖像的ID:
<script>
document.write(document.images[0].id);
</script></p>
</body>
</html>
<body>
<img id="zaixian1" border="0" src="klematis.jpg" width="150" height="113">
<img id="zaixian2" border="0" src="klematis2.jpg" width="152" height="128">
<p>第一個圖像的ID:
<script>
document.write(document.images[0].id);
</script></p>
</body>
</html>
以上實例輸出結果:
第一個圖像的ID:zaixian1
