HTML DOM linksコレクション
Document オブジェクト
定義と使い方
links コレクションは、現在のドキュメント内のすべてのリンクの配列を返します。
ヒント:links コレクションは、<a href=""> タグと <area> タグを数えます。
構文
document.links[].property
ブラウザサポート
![]()
![]()
![]()
![]()
![]()
すべての主要ブラウザが links コレクションをサポートしています。
例
例 1
ドキュメント内のリンク数を返します:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>サンプル(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">JavaScript 教程</a></p>
<p>链接/地址数目:
<script>
document.write(document.links.length);
</script></p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>サンプル(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">JavaScript 教程</a></p>
<p>链接/地址数目:
<script>
document.write(document.links.length);
</script></p>
</body>
</html>
上記の例の出力結果:
リンク/アドレス数: 4
試してみる »
例 2
ドキュメント内の最初のリンクを返します:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>サンプル(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">JavaScript 教程</a></p>
<p>最初のリンク/アドレスのID:
<script>
document.write(document.links[0].id);
</script></p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>サンプル(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">JavaScript 教程</a></p>
<p>最初のリンク/アドレスのID:
<script>
document.write(document.links[0].id);
</script></p>
</body>
</html>
上記の例の出力結果:
最初のリンク/アドレスのID:sun
試してみる »
Document オブジェクト その他の拡張