aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/outerhtml/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/api/element/outerhtml/index.html')
-rw-r--r--files/zh-cn/web/api/element/outerhtml/index.html172
1 files changed, 172 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/element/outerhtml/index.html b/files/zh-cn/web/api/element/outerhtml/index.html
new file mode 100644
index 0000000000..ae4983baec
--- /dev/null
+++ b/files/zh-cn/web/api/element/outerhtml/index.html
@@ -0,0 +1,172 @@
+---
+title: element.outerHTML
+slug: Web/API/Element/outerHTML
+tags:
+ - 'You Don''t Need jQuery & http://youmightnotneedjquery.com/'
+ - insertAdjacentHTML
+ - outerHTML
+translation_of: Web/API/Element/outerHTML
+---
+<p>{{APIRef("DOM")}}</p>
+
+<p> {{ domxref("element") }}  DOM接口的<code>outerHTML</code>属性获取描述元素(包括其后代)的序列化HTML片段。它也可以设置为用从给定字符串解析的节点替换元素。</p>
+
+<p>要仅获取元素内容的HTML表示形式或替换元素的内容,请使用 {{domxref("Element.innerHTML", "innerHTML")}} 属性</p>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="eval"><em>let content</em> = <em>element</em>.outerHTML;
+</pre>
+
+<p>返回时,内容包含描述元素及其后代的序列化HTML片段。</p>
+
+<pre class="eval"><em>element</em>.outerHTML = content;
+</pre>
+
+<p>将元素替换为通过使用元素的父作为片段解析算法的上下文节点解析字符串内容生成的节点。</p>
+
+<h2 id="Examples" name="Examples">例子</h2>
+
+<p>获取一个元素的outerHTML属性的值:</p>
+
+<pre class="brush: js">// HTML:
+/*
+&lt;div id="d"&gt;
+    &lt;p&gt;Content&lt;/p&gt;
+    &lt;p&gt;Further Elaborated&lt;/p&gt;
+&lt;/div&gt;
+*/
+
+const d = document.getElementById("d");
+console.log(d.outerHTML);
+
+/*
+ 字符串 '&lt;div id="d"&gt;&lt;p&gt;Content&lt;/p&gt;&lt;p&gt;Further Elaborated&lt;/p&gt;&lt;/div&gt;'
+ 被显示到控制台窗口
+*/
+</pre>
+
+<p>通过设置outerHTML属性来替换节点:</p>
+
+<pre class="brush: js">// HTML:
+/*
+&lt;div id="container"&gt;
+    &lt;div id="d"&gt;This is a div.&lt;/div&gt;
+&lt;/div&gt;
+*/
+
+let container = document.getElementById("container");
+let d = document.getElementById("d");
+
+console.log(container.firstChild.nodeName);
+// logs "div"
+
+d.outerHTML = "&lt;p&gt;This paragraph replaced the original div.&lt;/p&gt;";
+
+console.log(container.firstChild.nodeName);
+// logs "P"
+
+/*
+ #d div不再是文档树的一部分,新段替换了它。
+  <em>(不在页面中显示,但仍然在内存中) </em>
+*/
+</pre>
+
+<h2 id="注意事项">注意事项</h2>
+
+<p>如果元素没有父元素,即如果它是文档的根元素,则设置其outerHTML属性将抛出一个带有错误代码 <strong><em>NO_MODIFICATION_ALLOWED_ERR</em></strong> 的 <code><a href="/zh-cn/DOM/DOMException" title="DOMException">DOMException</a></code> 。例如:</p>
+
+<pre class="brush: js">document.documentElement.outerHTML = "test";
+// 抛出一个 DOMException
+</pre>
+
+<p>此外,虽然元素将在文档中被替换,设置了outerHTML属性的变量仍将保持对原始元素的引用:</p>
+
+<pre class="brush: js">let p = document.getElementsByTagName("p")[0];
+console.log(p.nodeName);
+// 显示: "P"
+p.outerHTML = "&lt;div&gt;This div replaced a paragraph.&lt;/div&gt;";
+
+console.log(p.nodeName);
+// 仍然为: "P";
+</pre>
+
+<p id="Specification"> </p>
+
+<h2 id="Specification" name="Specification" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">规范</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('DOM Parsing', '#outerhtml', 'Element.outerHTML')}}</td>
+ <td>{{ Spec2('DOM Parsing') }}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">浏览器兼容性</h2>
+
+<p>{{ CompatibilityTable() }}</p>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Firefox (Gecko)</th>
+ <th>Chrome</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{ CompatGeckoDesktop("11") }}</td>
+ <td>0.2</td>
+ <td>4.0</td>
+ <td>7</td>
+ <td>1.3</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatGeckoMobile("11") }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li>MSDN: <a class="external" href="http://msdn.microsoft.com/en-us/library/ms534310%28v=vs.85%29.aspx">outerHTML 属性</a></li>
+ <li><a href="http://youmightnotneedjquery.com/" rel="nofollow, noindex">You Don't Need jQuery </a></li>
+ <li><a href="http://youmightnotneedjquery.com/" rel="nofollow, noindex">http://youmightnotneedjquery.com/</a></li>
+ <li>insertAdjacentHTML</li>
+</ul>