aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/orphaned/web/api/childnode/replacewith/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/orphaned/web/api/childnode/replacewith/index.html')
-rw-r--r--files/zh-cn/orphaned/web/api/childnode/replacewith/index.html112
1 files changed, 112 insertions, 0 deletions
diff --git a/files/zh-cn/orphaned/web/api/childnode/replacewith/index.html b/files/zh-cn/orphaned/web/api/childnode/replacewith/index.html
new file mode 100644
index 0000000000..9f3ef5bd88
--- /dev/null
+++ b/files/zh-cn/orphaned/web/api/childnode/replacewith/index.html
@@ -0,0 +1,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>