Collection linksdu DOM HTML
Objet Document
Définition et utilisation
La collection links renvoie un tableau de tous les liens du document actuel.
Astuce :La collection links compte les balises <a href=""> et <area>.
Syntaxe
document.links[].property
Prise en charge des navigateurs
![]()
![]()
![]()
![]()
![]()
Tous les principaux navigateurs prennent en charge la collection links
Exemples
Exemple 1
Renvoie le nombre de liens dans le document :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutoriel d'exemple (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planètes" usemap ="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Soleil">
<area shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercure">
<area shape="circle" coords="124,58,8" href="venus.htm.html" alt="Vénus">
</map>
<p><a href="../js/index.html">Tutoriel JavaScript</a></p>
<p>Nombre de liens/adresses :
<script>
document.write(document.links.length);
</script></p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Tutoriel d'exemple (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planètes" usemap ="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Soleil">
<area shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercure">
<area shape="circle" coords="124,58,8" href="venus.htm.html" alt="Vénus">
</map>
<p><a href="../js/index.html">Tutoriel JavaScript</a></p>
<p>Nombre de liens/adresses :
<script>
document.write(document.links.length);
</script></p>
</body>
</html>
L'exemple ci-dessus produit :
Nombre de liens/adresses : 4
Essayez-le vous-même »
Exemple 2
Retourne le premier lien du document :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutoriel d'exemple (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planètes" usemap ="#planetmap">
<map name="planetmap">
<area id="sun" shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Soleil">
<area id="mercury" shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercure">
<area id="venus" shape="circle" coords="124,58,8" href="venus.htm.html" alt="Vénus">
</map>
<p><a id="javascript" href="../js/index.html">Tutoriel JavaScript</a></p>
<p>L'ID du premier lien/adresse :
<script>
document.write(document.links[0].id);
</script></p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Tutoriel d'exemple (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planètes" usemap ="#planetmap">
<map name="planetmap">
<area id="sun" shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Soleil">
<area id="mercury" shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercure">
<area id="venus" shape="circle" coords="124,58,8" href="venus.htm.html" alt="Vénus">
</map>
<p><a id="javascript" href="../js/index.html">Tutoriel JavaScript</a></p>
<p>L'ID du premier lien/adresse :
<script>
document.write(document.links[0].id);
</script></p>
</body>
</html>
L'exemple ci-dessus produit :
L'ID du premier lien/adresse : sun
Essayez-le vous-même »
Objet Document Autres extensions