アンカー(Anchor)

[up] [prev] [next]

■ 一覧

anchors, length, name, text, x, y

■ アンカーオブジェクト

window.document.anchors (e3/N2)
window.document.anchors.length (e3/N2)
window.document.anchors[n] (e3/N2)

anchors はこのドキュメントに含まれるアンカーオブジェクト(<a name="〜">〜</a> に相当します)の配列、length はその個数を示します。個々のオブジェクトは anchors[n] で参照します。

window.document.anchor.name (e4/N4)
window.document.anchor.text (N4)

<A NAME="○○">△△</A> において、name は ○○ の部分、text は △△ の部分に対応する文字列を示します。Netscape Communicator の場合、text で日本語を参照すると文字化けが発生したりするようです。Internet Explorer の場合は、text の代わりに innerText や innerHTML で参照可能です。

window.document.anchor.x (N4)
window.document.anchor.y (N4)

アンカーが画面に表示されている位置の x 座標と y 座標を示します。

■ 目次を自動生成する

下記の例では、ドキュメントに含まれるアンカーの一覧(目次)を自動生成します。(IE4.0 以降のみ)

<html>
<head>
<title>自動目次生成サンプル</title>
<script type="text/javascript">
function makeIndex() {
  if (!document.all) {
    return;
  }
  var str = "<ul>\n";
  for (var i = 0; i < document.anchors.length; i++) {
    var a = document.anchors[i];
    str += "<li><a href='" + document.location.pathname
        + "#" + a.name + "'>" + a.innerText + "</a>\n";
  }
  str += "</ul>\n";
  document.all.Contents.innerHTML = str;
}
</script>
</head>
<body onload="makeIndex()">
<h4>■ 目次</h4>
<div id="Contents">
</div>
<h4><a name="Hajimeni">■ はじめに</a></h4>
   :

[up] [prev] [next]
Copyright (C) 1996-2001 杜甫々
改訂版初版:2001年5月20日
http://www.tohoho-web.com/js/anchors.htm