aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/api/document/anchors/index.md
blob: d023a618962f70574adedf9e4ec0580b4a221d21 (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
---
title: Document.anchors
slug: Web/API/Document/anchors
translation_of: Web/API/Document/anchors
original_slug: Web/API/Document/Document.anchors
---
{{APIRef("DOM")}} {{deprecated_header()}}

`anchors` retourne une liste de toutes les ancres du document.

## Syntaxe

    nodeList = document.anchors;

## Exemple

```js
if ( document.anchors.length >= 5 ) {
  dump("Trop d'ancres trouvées !");
  window.location = "http://www.google.com";
}
```

L'exemple suivant remplit un tableau avec chaque ancre présente sur la page :

```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>
```

[Voir dans JSFiddle](https://jsfiddle.net/S4yNp)

## Notes

Pour des raisons de rétrocompatibilité, la liste d'ancres retournée contient seulement les ancres créées avec l'attribut `name`, pas celles créées avec l'attribut `id`.

## Spécification

- [DOM Level 2 HTML: anchors](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-7577272)