aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/replacewith/index.html
blob: 9f3ef5bd887fad6e5fc79978fe426cba18b1f23b (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
108
109
110
111
112
---
title: ChildNode.replaceWith()
slug: orphaned/Web/API/ChildNode/replaceWith
tags:
  - DOM
  - Node
translation_of: Web/API/ChildNode/replaceWith
original_slug: Web/API/ChildNode/replaceWith
---
<div>{{APIRef("DOM")}} {{SeeCompatTable}}</div>

<p><code><strong>ChildNode</strong></code><strong><code>.replaceWith()</code></strong> 的方法用一套 {{domxref("Node")}} 对象或者 {{domxref("DOMString")}} 对象,替换了该节点父节点下的子节点 。{{domxref("DOMString")}} 对象被当做等效的{{domxref("Text")}} 节点插入。</p>

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

<pre class="syntaxbox">[Throws, Unscopable]
void ChildNode.replaceWith((Node or DOMString)... nodes);
</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>节点</code></dt>
 <dd>一系列用来替换的{{domxref("Node")}} 对象或者 {{domxref("DOMString")}} 对象。</dd>
</dl>

<h3 id="例外">例外</h3>

<ul>
 <li>{{domxref("HierarchyRequestError")}}: 无法在层次结构中的指定点插入节点。</li>
</ul>

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

<h3 id="Using_replaceWith">Using <code>replaceWith()</code></h3>

<pre class="brush: js">var parent = document.createElement("div");
var child = document.createElement("p");
parent.appendChild(child);
var span = document.createElement("span");

child.replaceWith(span);

console.log(parent.outerHTML);
// "&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;"
</pre>

<h3 id="ChildNode.replaceWith_is_unscopable"><code>ChildNode.replaceWith()</code> is unscopable</h3>

<p><code>replaceWith()</code>的方法并没有作用于with语句. 参考 {{jsxref("Symbol.unscopables")}} 获取更多信息.</p>

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

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

<p>你可以在IE9及更高级的浏览器中使用下面的代码向上兼容<code>replaceWith()</code>的方法:</p>

<pre class="brush: js">(function (arr) {
  arr.forEach(function (item) {
    if (item.hasOwnProperty('replaceWith')) {
      return;
    }
    Object.defineProperty(item, 'replaceWith', {
      configurable: true,
      enumerable: true,
      writable: true,
      value: function replaceWith() {
        var argArr = Array.prototype.slice.call(arguments),
          docFrag = document.createDocumentFragment();

        argArr.forEach(function (argItem) {
          var isNode = argItem instanceof Node;
          docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
        });

        this.parentNode.replaceChild(docFrag, this);
      }
    });
  });
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);</pre>

<h2 id="规范">规范</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-replacewith', 'ChildNode.replacewith()')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

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

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

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

<ul>
 <li>{{domxref("ChildNode")}}{{domxref("ParentNode")}}</li>
 <li>{{domxref("Node.replaceChild()")}}</li>
 <li>{{domxref("NodeList")}}</li>
</ul>