blob: 3add5444121c43a310cdb99b2537c773893c6b12 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
---
title: Document.anchors
slug: Web/API/Document/anchors
tags:
- API
- DOM
- Deprecated
- Document
- HTML DOM
- Property
- Reference
- anchors
- プロパティ
translation_of: Web/API/Document/anchors
---
<div>{{APIRef("DOM")}} {{deprecated_header()}}</div>
<p><strong><code>anchors</code></strong> は {{domxref("Document")}} インターフェイスの読み取り専用のプロパティで、文書中のすべてのアンカーのリストを返します。</p>
<h2 id="Syntax" name="Syntax">構文</h2>
<pre class="syntaxbox"><var>nodeList</var> = document.anchors;
</pre>
<h3 id="Value" name="Value">値</h3>
<p>{{domxref("HTMLCollection")}} です。</p>
<h2 id="Example" name="Example">例</h2>
<pre class="brush:js">if (document.anchors.length >= 5) {
dump("dump found too many anchors");
window.location = "http://www.google.com";
}
</pre>
<p>文書中のアンカーを基に目次を作成して文書に挿入する例を以下に示します。</p>
<pre class="brush:html"><!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>
</pre>
<p><a href="https://jsfiddle.net/S4yNp">JSFiddle で確認</a></p>
<h2 id="Notes" name="Notes">メモ</h2>
<p>後方互換性のため、返されるアンカーのセットには <code>name</code> 属性を付けて作成されたアンカーのみが含まれ、 <code>id</code> 属性付きで作成されたものは含まれません。</p>
<h2 id="Specifications" name="Specifications">仕様書</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">仕様書</th>
<th scope="col">状態</th>
<th scope="col">備考</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('HTML WHATWG', '#dom-document-anchors', 'Document.anchors')}}</td>
<td>{{ Spec2('HTML WHATWG') }}</td>
<td>廃止</td>
</tr>
<tr>
<td>{{SpecName('DOM2 HTML', 'html.html#ID-7577272', 'Document.anchors')}}</td>
<td>{{ Spec2('DOM2 Events') }}</td>
<td>初回定義</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの対応</h2>
<div>{{Compat("api.Document.anchors")}}</div>
|