diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/node/replacechild | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/node/replacechild')
-rw-r--r-- | files/zh-cn/web/api/node/replacechild/index.html | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/node/replacechild/index.html b/files/zh-cn/web/api/node/replacechild/index.html new file mode 100644 index 0000000000..e346fcb0c2 --- /dev/null +++ b/files/zh-cn/web/api/node/replacechild/index.html @@ -0,0 +1,99 @@ +--- +title: Node.replaceChild() +slug: Web/API/Node/replaceChild +tags: + - API + - DOM + - Node + - 参考 + - 方法 +translation_of: Web/API/Node/replaceChild +--- +<div>{{APIRef("DOM")}}</div> + +<p><strong><code>Node.replaceChild()</code></strong> 方法用指定的节点替换当前节点的一个子节点,并返回被替换掉的节点。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>parentNode</var>.replaceChild(<var>newChild</var>, <var>oldChild</var>); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code><strong>newChild</strong></code></dt> + <dd>用来替换 <code>oldChild</code> 的新节点。如果该节点已经存在于 DOM 树中,则它首先会被从原始位置删除。</dd> + <dt><code><strong>oldChild</strong></code></dt> + <dd>被替换掉的原始节点。</dd> +</dl> + +<ul> +</ul> + +<h3 id="返回值">返回值</h3> + +<p>The returned value is the replaced node. This is the same node as <code>oldChild</code>.</p> + +<h2 id="例子">例子</h2> + +<pre class="brush: js">// <div> +// <span id="childSpan">foo bar</span> +// </div> + +// 创建一个空的span元素节点 +// 没有id,没有任何属性和内容 +var sp1 = document.createElement("span"); + +// 添加一个id属性,值为'newSpan' +sp1.setAttribute("id", "newSpan"); + +// 创建一个文本节点 +var sp1_content = document.createTextNode("新的span元素的内容."); + +// 将文本节点插入到span元素中 +sp1.appendChild(sp1_content); + +// 获得被替换节点和其父节点的引用. +var sp2 = document.getElementById("childSpan"); +var parentDiv = sp2.parentNode; + +// 用新的span元素sp1来替换掉sp2 +parentDiv.replaceChild(sp1, sp2); + +// 结果: +// <div> +// <span id="newSpan">新的span元素的内容.</span> +// </div> +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName("DOM WHATWG", "#dom-node-replacechild", "Node: replaceChild")}}</td> + <td>{{Spec2("DOM WHATWG")}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.Node.replaceChild")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{domxref("Node.removeChild")}}</li> + <li>{{domxref("ChildNode.replaceWith")}}</li> +</ul> |