From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/ja/web/api/document/anchors/index.html | 117 +++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 files/ja/web/api/document/anchors/index.html (limited to 'files/ja/web/api/document/anchors') diff --git a/files/ja/web/api/document/anchors/index.html b/files/ja/web/api/document/anchors/index.html new file mode 100644 index 0000000000..5ea288b926 --- /dev/null +++ b/files/ja/web/api/document/anchors/index.html @@ -0,0 +1,117 @@ +--- +title: Document.anchors +slug: Web/API/Document/anchors +tags: + - API + - DOM + - Deprecated + - Document + - HTML DOM + - Property + - Reference + - anchors + - プロパティ +translation_of: Web/API/Document/anchors +--- +
{{APIRef("DOM")}} {{deprecated_header()}}
+ +

anchors は {{domxref("Document")}} インターフェイスの読み取り専用のプロパティで、文書中のすべてのアンカーのリストを返します。

+ +

構文

+ +
nodeList = document.anchors;
+
+ +

+ +

{{domxref("HTMLCollection")}} です。

+ +

+ +
if (document.anchors.length >= 5) {
+  dump("dump found too many anchors");
+  window.location = "http://www.google.com";
+}
+
+ +

文書中のアンカーを基に目次を作成して文書に挿入する例を以下に示します。

+ +
<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="UTF-8" />
+<title>Test</title>
+<script>
+function init() {
+  var toc = document.getElementById("toc");
+  var i, li, newAnchor;
+  for (i = 0; i < document.anchors.length; i++) {
+    li = document.createElement("li");
+    newAnchor = document.createElement('a');
+    newAnchor.href = "#" + document.anchors[i].name;
+    newAnchor.innerHTML = document.anchors[i].text;
+    li.appendChild(newAnchor);
+    toc.appendChild(li);
+  }
+}
+</script>
+</head>
+<body onload="init()">
+
+<h1>Title</h1>
+<h2><a name="contents">Contents</a></h2>
+<ul id="toc"></ul>
+
+<h2><a name="plants">Plants</a></h2>
+<ol>
+  <li>Apples</li>
+  <li>Oranges</li>
+  <li>Pears</li>
+</ol>
+
+<h2><a name="veggies">Veggies</a></h2>
+<ol>
+  <li>Carrots</li>
+  <li>Celery</li>
+  <li>Beats</li>
+</ol>
+
+</body>
+</html>
+
+ +

JSFiddle で確認

+ +

メモ

+ +

後方互換性のため、返されるアンカーのセットには name 属性を付けて作成されたアンカーのみが含まれ、 id 属性付きで作成されたものは含まれません。

+ +

仕様書

+ + + + + + + + + + + + + + + + + + + + + +
仕様書状態備考
{{SpecName('HTML WHATWG', '#dom-document-anchors', 'Document.anchors')}}{{ Spec2('HTML WHATWG') }}廃止
{{SpecName('DOM2 HTML', 'html.html#ID-7577272', 'Document.anchors')}}{{ Spec2('DOM2 Events') }}初回定義
+ +

ブラウザーの対応

+ + + +
{{Compat("api.Document.anchors")}}
-- cgit v1.2.3-54-g00ecf