blob: 52c529a1b89821e4a5896900257b57d4ccdeafe8 (
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
|
---
title: ParentNode.children
slug: Web/API/ParentNode/children
tags:
- API
- DOM
- Noeuds
- Propriétés
- parent
translation_of: Web/API/ParentNode/children
---
<p>{{ APIRef("DOM") }}</p>
<p>La propriété <code><strong>children</strong></code> de {{domxref("ParentNode")}} est une propriété en lecture seule qui renvoie une {{domxref("HTMLCollection")}} directe contenant tous les enfants {{domxref("Element","éléments")}} du noeud sur lequel elle a été appelée.</p>
<h2 id="Syntaxe">Syntaxe</h2>
<pre class="syntaxbox">var <em>children</em> = <em>node</em>.children;</pre>
<h3 id="Valeur">Valeur</h3>
<p>Une {{ domxref("HTMLCollection") }}, qui est une collection directe et ordonnée des éléments DOM qui sont enfants du <em><code>node</code></em> (<em>noeud</em>). Vous pouvez accéder aux noeuds enfants individuellement en utilisant la méthode {{domxref("HTMLCollection.item", "item()")}} (<em>élément</em>) sur la collection ou en utilisant la notation de type tableau (<em>array</em>) de JavaScript.</p>
<p>S'il n'y a pas d'éléments enfants, alors <code>children</code> est une liste vide et a une <code>length</code> (<em>longueur</em>) de <code>0</code>.</p>
<h2 id="Exemple">Exemple</h2>
<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="keyword token">var</span> machin <span class="operator token">=</span> document<span class="punctuation token">.</span><span class="function token">getElementById</span><span class="punctuation token">(</span><span class="string token">'machin'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> i <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">;</span> i <span class="operator token"><</span> machin<span class="punctuation token">.</span>children<span class="punctuation token">.</span>length<span class="punctuation token">;</span> i<span class="operator token">++</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>machin<span class="punctuation token">.</span>children<span class="punctuation token">[</span>i<span class="punctuation token">]</span><span class="punctuation token">.</span>tagName<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span></code></pre>
<h2 id="Polyfill">Polyfill</h2>
<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="comment token">// Réécrit le prototype 'children' natif.</span>
<span class="comment token">// Ajoute le support de Document & DocumentFragment pour IE9 & Safari.</span>
<span class="comment token">// Renvoie un tableau (<em>array</em>) au lieu d'une HTMLCollection.</span>
<span class="punctuation token">;</span><span class="punctuation token">(</span><span class="keyword token">function</span><span class="punctuation token">(</span>constructeur<span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span>constructeur <span class="operator token">&&</span>
constructeur<span class="punctuation token">.</span>prototype <span class="operator token">&&</span>
constructeur<span class="punctuation token">.</span>prototype<span class="punctuation token">.</span>children <span class="operator token">==</span> <span class="keyword token">null</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
Object<span class="punctuation token">.</span><span class="function token">defineProperty</span><span class="punctuation token">(</span>constructeur<span class="punctuation token">.</span>prototype<span class="punctuation token">,</span> <span class="string token">'children'</span><span class="punctuation token">,</span> <span class="punctuation token">{</span>
<span class="keyword token">get</span><span class="punctuation token">:</span> <span class="keyword token">function</span><span class="punctuation token">(</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="keyword token">var</span> i <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">,</span> noeud<span class="punctuation token">,</span> noeuds <span class="operator token">=</span> <span class="keyword token">this</span><span class="punctuation token">.</span>childNodes<span class="punctuation token">,</span> children <span class="operator token">=</span> <span class="punctuation token">[</span><span class="punctuation token">]</span><span class="punctuation token">;</span>
<span class="keyword token">while</span> <span class="punctuation token">(</span>noeud <span class="operator token">=</span> noeuds<span class="punctuation token">[</span>i<span class="operator token">++</span><span class="punctuation token">]</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span>noeud<span class="punctuation token">.</span>nodeType <span class="operator token">===</span> <span class="number token">1</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
children<span class="punctuation token">.</span><span class="function token">push</span><span class="punctuation token">(</span>noeud<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>
<span class="punctuation token">}</span>
<span class="keyword token">return</span> children<span class="punctuation token">;</span>
<span class="punctuation token">}</span>
<span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>
<span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">(</span>window<span class="punctuation token">.</span>Node <span class="operator token">||</span> window<span class="punctuation token">.</span>Element<span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre>
<h2 id="Spécification">Spécification</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spécification</th>
<th scope="col">Statut</th>
<th scope="col">Commentaire</th>
</tr>
<tr>
<td>{{SpecName('DOM WHATWG', '#dom-parentnode-children', 'ParentNode.children')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td>Définition initiale.</td>
</tr>
</tbody>
</table>
<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2>
<p>{{Compat("api.ParentNode.children")}}</p>
<h2 id="Voir_aussi">Voir aussi</h2>
<ul>
<li>Les interfaces {{domxref("ParentNode")}} et{{domxref("ChildNode")}}.</li>
<li>
<div class="syntaxbox">Les types d'objets implémentant cette interface : {{domxref("Document")}}, {{domxref("Element")}} et {{domxref("DocumentFragment")}}.</div>
</li>
<li>
<div class="syntaxbox">{{domxref("Node.childNodes")}}</div>
</li>
</ul>
|