aboutsummaryrefslogtreecommitdiff
path: root/files/ru/web/api/document/anchors/index.html
blob: aa7c353b59be2c057b5f2b2d77552128fb001e6a (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
116
117
118
119
120
121
---
title: Document.anchors
slug: Web/API/Document/anchors
tags:
  - API
  - Document
  - HTML DOM
  - id
  - name
  - Якоря
translation_of: Web/API/Document/anchors
---
<div>{{APIRef("DOM")}}</div>

<p><strong><code>anchors</code></strong> возвращает массив всех якорей в документе.</p>

<h2 id="Syntax">Синтаксис</h2>

<pre class="syntaxbox"><var>nodeList</var> = document.anchors;
</pre>

<p> </p>

<h3 id="Значение">Значение</h3>

<p>{{domxref("HTMLCollection")}}.</p>

<p> </p>

<h2 id="Example">Пример</h2>

<pre class="brush:js">if ( document.anchors.length &gt;= 5 ) {
  dump("найдено слишком много якорей");
  window.location = "http://www.google.com";
}
</pre>

<p>Следующий пример автоматически генерирует список якорей из заглавий блоков, имеющихся на странице:</p>

<pre class="brush:html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8" /&gt;
&lt;title&gt;Test&lt;/title&gt;
&lt;script&gt;
function init() {
  var toc = document.getElementById("toc");
  var i, li, newAnchor;
  for (i = 0; i &lt; 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);
  }
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body onload="init()"&gt;

&lt;h1&gt;Title&lt;/h1&gt;
&lt;h2&gt;&lt;a name="contents"&gt;Contents&lt;/a&gt;&lt;/h2&gt;
&lt;ul id="toc"&gt;&lt;/ul&gt;

&lt;h2&gt;&lt;a name="plants"&gt;Plants&lt;/a&gt;&lt;/h2&gt;
&lt;ol&gt;
    &lt;li&gt;Apples&lt;/li&gt;
    &lt;li&gt;Oranges&lt;/li&gt;
    &lt;li&gt;Pears&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;&lt;a name="veggies"&gt;Veggies&lt;/a&gt;&lt;/h2&gt;
&lt;ol&gt;
    &lt;li&gt;Carrots&lt;/li&gt;
    &lt;li&gt;Celery&lt;/li&gt;
    &lt;li&gt;Beats&lt;/li&gt;
&lt;/ol&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>

<p><a href="https://jsfiddle.net/S4yNp">Посмотреть на JSFiddle</a></p>

<h2 id="Notes">Примечание</h2>

<p>По причине обратной совместимости возвращаемый массив якорей включает в себя лишь якоря, созданные с помощью атрибута <strong>name</strong>, а не а <strong>id</strong>.</p>

<p> </p>

<h2 id="Спецификации">Спецификации</h2>

<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>Obsoleted.</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM2 HTML', 'html.html#ID-7577272', 'Document.anchors')}}</td>
   <td>{{ Spec2('DOM2 Events') }}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Браузерная_поддержка">Браузерная поддержка</h2>

<p>{{Compat("api.Document.anchors")}}</p>

<p> </p>