HTML DOM imagesColección
Objeto Document
Definición y uso
La colección images devuelve una matriz (array) de todas las imágenes del documento actual.
Sintaxis
document.images[].property
Soporte del navegador
![]()
![]()
![]()
![]()
![]()
Todos los navegadores principales soportan la colección images
Ejemplos
Ejemplo 1
Devuelve el número de imágenes del documento:
<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>Número de imágenes:
<script>
document.write(document.images.length);
</script></p>
</body>
</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>Número de imágenes:
<script>
document.write(document.images.length);
</script></p>
</body>
</html>
El resultado del ejemplo anterior es:
Número de imágenes: 2
Pruébalo »
Ejemplo 2
Devuelve la primera imagen del documento:
<html>
<body>
<img id="ejemplo1" border="0" src="klematis.jpg.html" width="150" height="113">
<img id="ejemplo2" border="0" src="klematis2.jpg.html" width="152" height="128">
<p>ID de la primera imagen:
<script>
document.write(document.images[0].id);
</script></p>
</body>
</html>
<body>
<img id="ejemplo1" border="0" src="klematis.jpg.html" width="150" height="113">
<img id="ejemplo2" border="0" src="klematis2.jpg.html" width="152" height="128">
<p>ID de la primera imagen:
<script>
document.write(document.images[0].id);
</script></p>
</body>
</html>
El resultado del ejemplo anterior es:
ID de la primera imagen: ejemplo1
Pruébalo »
Objeto Document Otras extensiones