aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/childnode/remove/index.html
blob: 001975cd60ff8422cd778362398b114a2b7fd6e4 (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
---
title: ChildNode.remove()
slug: Web/API/ChildNode/remove
tags:
  - API
  - ChildNode
  - DOM
  - Method
translation_of: Web/API/ChildNode/remove
---
<p>{{APIRef("DOM")}}</p>

<p><code><strong>ChildNode.remove()</strong></code> 方法,把对象从它所属的 DOM 树中删除。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><var>node</var>.remove();</pre>

<h2 id="示例">示例</h2>

<h3 id="使用_remove()">使用 <code>remove()</code></h3>

<pre class="brush: html">&lt;div id="div-01"&gt;Here is div-01&lt;/div&gt;
&lt;div id="div-02"&gt;Here is div-02&lt;/div&gt;
&lt;div id="div-03"&gt;Here is div-03&lt;/div&gt;
</pre>

<pre class="brush: js">var el = document.getElementById('div-02');
el.remove();
// id 为 'div-02' 的 div 被删掉了
</pre>

<p>{{EmbedLiveSample('使用_remove()')}}</p>

<h3 id="ChildNode.remove()_是不可见的"><code>ChildNode.remove()</code> 是不可见的</h3>

<p><code>with</code> 语句中,<code>remove()</code> 方法是不可见的。参阅 {{jsxref("Symbol.unscopables")}} 了解更多信息。</p>

<pre class="brush: js">with(node) {
  remove();
}
// ReferenceError: remove is not defined</pre>

<h2 id="Polyfill">Polyfill</h2>

<p>You can polyfill the <code>remove()</code> method in Internet Explorer 9 and higher with the following code:</p>

<pre class="brush: js">//https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md
(function (arr) {
   arr.forEach(function (item) {
    if (item.hasOwnProperty('remove')) {
      return;
    }
    Object.defineProperty(item, 'remove', {
      configurable: true,
      enumerable: true,
      writable: true,
      value: function remove() {
        this.parentNode.removeChild(this);
      }
    });
  });
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);</pre>

<h2 id="Specification" name="Specification">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">注释</th>
  </tr>
  <tr>
   <td>{{SpecName('DOM WHATWG', '#dom-childnode-remove', 'ChildNode.remove')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.ChildNode.remove")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li>{{domxref("ChildNode")}} 纯接口。</li>
 <li>
  <div class="syntaxbox">实现此纯接口的对象类型: {{domxref("CharacterData")}}{{domxref("Element")}} , 和 {{domxref("DocumentType")}}.</div>
 </li>
</ul>