blob: ea111ec412bc02d2bdfeb1c73079750d7390ca54 (
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
|
---
title: Element.after()
slug: Web/API/Element/after
translation_of: Web/API/Element/after
original_slug: Web/API/ChildNode/after
tags:
- API
- DOM
- Method
- Node
- Reference
browser-compat: api.Element.after
---
<div>{{APIRef("DOM")}}</div>
<p><strong><code>Element.after()</code> </strong>方法会在其父节点的子节点列表中插入一些 {{domxref("Node")}} 或 {{domxref("DOMString")}} 对象。插入位置为该节点之后。{{domxref("DOMString")}} 对象会被以 {{domxref("Text")}} 的形式插入。</p>
<h2 id="Syntax">语法</h2>
<pre class="brush: js">
after(... nodes)
</pre>
<h3 id="Parameters">参数</h3>
<dl>
<dt><code>nodes</code></dt>
<dd>一组准备插入的 {{domxref("Node")}} 或 {{domxref("DOMString")}} 。</dd>
</dl>
<h3 id="Exceptions">错误</h3>
<ul>
<li>{{domxref("HierarchyRequestError")}}: 在某些不正确的层级结构进行了插入操作。</li>
</ul>
<h2 id="Examples">示例</h2>
<h3 id="Inserting_an_element">插入元素</h3>
<pre class="brush: js">let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
let span = document.createElement("span");
p.after(span);
console.log(container.outerHTML);
// "<div><p></p><span></span></div>"
</pre>
<h3 id="Inserting_text">插入文本</h3>
<pre class="brush: js">let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
p.after("Text");
console.log(container.outerHTML);
// "<div><p></p>Text</div>"</pre>
<h3 id="Inserting_an_element_and_text">同时插入元素和文本</h3>
<pre class="brush: js">let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
let span = document.createElement("span");
p.after(span, "Text");
console.log(container.outerHTML);
// "<div><p></p><span></span>Text</div>"</pre>
<h2 id="Specifications">规范</h2>
{{Specifications}}
<h2 id="Browser_compatibility">浏览器兼容性</h2>
<p>{{Compat}}</p>
<h2 id="See_also">相关链接</h2>
<ul>
<li>{{domxref("Element.before()")}}</li>
<li>{{domxref("Element.append()")}}</li>
<li>{{domxref("Node.appendChild()")}}</li>
<li>{{domxref("Element.insertAdjacentElement()")}}</li>
<li>{{domxref("NodeList")}}</li>
</ul>
|