From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- files/ru/web/api/document/anchors/index.html | 125 +++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 files/ru/web/api/document/anchors/index.html (limited to 'files/ru/web/api/document/anchors') diff --git a/files/ru/web/api/document/anchors/index.html b/files/ru/web/api/document/anchors/index.html new file mode 100644 index 0000000000..9d9f3aaa70 --- /dev/null +++ b/files/ru/web/api/document/anchors/index.html @@ -0,0 +1,125 @@ +--- +title: Document.anchors +slug: Web/API/Document/anchors +tags: + - API + - Document + - HTML DOM + - id + - name + - Якоря +translation_of: Web/API/Document/anchors +--- +
{{APIRef("DOM")}}
+ +

anchors возвращает массив всех якорей в документе.

+ +

Синтаксис

+ +
nodeList = document.anchors;
+
+ +

 

+ +

Значение

+ +

{{domxref("HTMLCollection")}}.

+ +

 

+ +

Пример

+ +
if ( document.anchors.length >= 5 ) {
+  dump("найдено слишком много якорей");
+  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') }}Obsoleted.
{{SpecName('DOM2 HTML', 'html.html#ID-7577272', 'Document.anchors')}}{{ Spec2('DOM2 Events') }}Initial definition.
+ +

Браузерная поддержка

+ + + +

{{Compat("api.Document.anchors")}}

+ +

 

-- cgit v1.2.3-54-g00ecf