HTML DOM linksColección
Objeto Document
Definición y uso
La colección links devuelve una matriz de todos los enlaces del documento actual.
Consejo:La colección links cuenta las etiquetas <a href=""> y las etiquetas <area>.
Sintaxis
document.links[].property
Soporte del navegador
![]()
![]()
![]()
![]()
![]()
Todos los navegadores principales son compatibles con la colección links.
Ejemplos
Ejemplo 1
Devuelve el número de enlaces del documento:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial de novatos (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm.html" alt="Venus">
</map>
<p><a href="../js/index.html">Tutorial de JavaScript</a></p>
<p>Número de enlaces/direcciones:
<script>
document.write(document.links.length);
</script></p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial de novatos (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm.html" alt="Venus">
</map>
<p><a href="../js/index.html">Tutorial de JavaScript</a></p>
<p>Número de enlaces/direcciones:
<script>
document.write(document.links.length);
</script></p>
</body>
</html>
El resultado del ejemplo anterior:
Número de enlaces/direcciones: 4
Pruébalo »
Ejemplo 2
Devuelve el primer enlace del documento:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial de novatos (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area id="sun" shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Sun">
<area id="mercury" shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercury">
<area id="venus" shape="circle" coords="124,58,8" href="venus.htm.html" alt="Venus">
</map>
<p><a id="javascript" href="../js/index.html">Tutorial de JavaScript</a></p>
<p>ID del primer enlace/dirección:
<script>
document.write(document.links[0].id);
</script></p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial de novatos (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area id="sun" shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Sun">
<area id="mercury" shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercury">
<area id="venus" shape="circle" coords="124,58,8" href="venus.htm.html" alt="Venus">
</map>
<p><a id="javascript" href="../js/index.html">Tutorial de JavaScript</a></p>
<p>ID del primer enlace/dirección:
<script>
document.write(document.links[0].id);
</script></p>
</body>
</html>
El resultado del ejemplo anterior:
ID del primer enlace/dirección: sun
Pruébalo »
Objeto Document Otras extensiones