blob: ad0b5ffa1c92739ed694a3377159b85fe0af65f9 (
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
|
---
title: ChildNode.remove()
slug: Web/API/ChildNode/remove
tags:
- API
- DOM
- Méthodes
- Noeud Enfant
translation_of: Web/API/ChildNode/remove
---
<div>{{APIRef("DOM")}}</div>
<p>La méthode <code><strong>ChildNode.remove()</strong></code> retire l'objet de l'arbre auquel il appartient.</p>
<h2 id="Syntaxe">Syntaxe</h2>
<pre class="syntaxbox"><em>node</em>.remove();
</pre>
<h2 id="Exemple">Exemple</h2>
<h3 id="Utilisation_de_la_méthode_remove">Utilisation de la méthode <code>remove()</code></h3>
<pre class="brush: html"><div id="div-01">Here is div-01</div>
<div id="div-02">Here is div-02</div>
<div id="div-03">Here is div-03</div>
</pre>
<pre class="brush: js">var el = document.getElementById('div-01');
el.nextElementSibling.remove(); // Retire l'élément div dont l'id est 'div-02'</pre>
<h3 id="ChildNode.remove_est_unscopable"><code>ChildNode.remove()</code> est <em>unscopable</em></h3>
<p>La méthode <code>remove()</code> n'est pas comprise dans une instruction <code>with</code>. Voir {{jsxref("Symbol.unscopables")}} pour plus d'informations.</p>
<pre class="brush: js">with(node) {
remove();
}
// ReferenceError: remove is not defined (<em>remove n'est pas défini</em>)</pre>
<h2 id="Prothèse_démulation">Prothèse d'émulation</h2>
<p>Vous pouvez utiliser une prothèse d'émulation pour la méthode <code>remove()</code> dans Internet Explorer 9 et supérieur avec le code suivant :</p>
<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="comment token">// from:https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md</span>
<span class="punctuation token">(</span><span class="keyword token">function</span> <span class="punctuation token">(</span>arr<span class="punctuation token">)</span> <span class="punctuation token">{</span>
arr<span class="punctuation token">.</span><span class="function token">forEach</span><span class="punctuation token">(</span><span class="keyword token">function</span> <span class="punctuation token">(</span>item<span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span>item<span class="punctuation token">.</span><span class="function token">hasOwnProperty</span><span class="punctuation token">(</span><span class="string token">'remove'</span><span class="punctuation token">)</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="keyword token">return</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>item<span class="punctuation token">,</span> <span class="string token">'remove'</span><span class="punctuation token">,</span> <span class="punctuation token">{</span>
configurable<span class="punctuation token">:</span> <span class="keyword token">true</span><span class="punctuation token">,</span>
enumerable<span class="punctuation token">:</span> <span class="keyword token">true</span><span class="punctuation token">,</span>
writable<span class="punctuation token">:</span> <span class="keyword token">true</span><span class="punctuation token">,</span>
value<span class="punctuation token">:</span> <span class="keyword token">function</span> <span class="function token">remove</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><span class="keyword token">this</span><span class="punctuation token">.</span>parentNode <span class="operator token">!==</span> <span class="keyword token">null</span><span class="punctuation token">)</span>
<span class="keyword token">this</span><span class="punctuation token">.</span>parentNode<span class="punctuation token">.</span><span class="function token">removeChild</span><span class="punctuation token">(</span><span class="keyword token">this</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><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>Element<span class="punctuation token">.</span>prototype<span class="punctuation token">,</span> CharacterData<span class="punctuation token">.</span>prototype<span class="punctuation token">,</span> DocumentType<span class="punctuation token">.</span>prototype<span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre>
<h2 id="Spécifications">Spécifications</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-childnode-remove', 'ChildNode.remove')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td>Définition initiale</td>
</tr>
</tbody>
</table>
<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2>
<div>{{Compat("api.ChildNode.remove")}}</div>
<h2 id="Voir_aussi">Voir aussi</h2>
<ul>
<li>L'interface simple {{domxref("ChildNode")}}.</li>
<li>
<div class="syntaxbox">Les types d'objets implémentant cette interface simple : {{domxref("CharacterData")}}, {{domxref("Element")}} et {{domxref("DocumentType")}}.</div>
</li>
</ul>
|