blob: 0b86ad57753818e39a05c089b2637824ecd9f237 (
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
---
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:
/*
<div id="d">
<p>Content</p>
<p>Further Elaborated</p>
</div>
*/
const d = document.getElementById("d");
console.log(d.outerHTML);
/*
字符串 '<div id="d"><p>Content</p><p>Further Elaborated</p></div>'
被显示到控制台窗口
*/
</pre>
<p>通过设置outerHTML属性来替换节点:</p>
<pre class="brush: js">// HTML:
/*
<div id="container">
<div id="d">This is a div.</div>
</div>
*/
let container = document.getElementById("container");
let d = document.getElementById("d");
console.log(container.firstChild.nodeName);
// logs "div"
d.outerHTML = "<p>This paragraph replaced the original div.</p>";
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 = "<div>This div replaced a paragraph.</div>";
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>
{{Compat("api.Element.outerHTML")}}
<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>
|