blob: 624c955b0e806613e31d79aa590965013c4fd2cc (
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
|
---
title: Document.anchors
slug: Web/API/Document/anchors
tags:
- API
- Deprecated
- HTML DOM
- NeedsCompatTable
- Property
- Reference
translation_of: Web/API/Document/anchors
---
<div>{{APIRef("DOM")}}</div>
<p>{{deprecated_header("HTML5")}}</p>
<p><code>anchors</code> retorna uma lista de todas as âncoras no documento.</p>
<h2 id="Syntax" name="Syntax">Sintaxe</h2>
<pre class="syntaxbox"><var>nodeList</var> = document.anchors;
</pre>
<h2 id="Example" name="Example">Exemplo</h2>
<pre class="brush:js">if ( document.anchors.length >= 5 ) {
dump("dump found too many anchors");
window.location = "http://www.google.com";
}
</pre>
<p>O código a seguir é um exemplo que popula automaticamente um índice de conteúdo com cada âncora encontrada na página:</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">View on JSFiddle</a></p>
<h2 id="Notes" name="Notes">Notas</h2>
<p>Por motivos de compatibilidade, o conjunto de âncoras retornadas por <code>anchors</code> contém apenas as âncoras criadas com o atributo <code>name</code>, não incluindo as âncoras criadas com o atributo {{ htmlattrxref("id") }}.</p>
<h2 id="Specifications" name="Specifications">Especificações</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</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>
|