下面的代码通过document.images获取网页中的所有图片,然后获取第一个图片的id属性
1 2 3 4 5 6 7 8 9 10 11 12 13 <!DOCTYPE html> <html> <body> <img id="klematis lilac" border="0" src="klematis.jpg" width="148" height="112"> <img id="klematis pink" border="0" src="klematis2.jpg" width="149" height="118"> <p>Id of first image: <script> document.write(document.images[0].id); </script></p> </body> </html>运行结果如下:
1 Id of first image:klematis lilac