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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
---
title: ChildNode.before()
slug: orphaned/Web/API/ChildNode/before
tags:
- api、dom、节点、方法
translation_of: Web/API/ChildNode/before
original_slug: Web/API/ChildNode/before
---
<div>{{APIRef("DOM")}} {{SeeCompatTable}}</div>
<p> <code><strong>ChildNode</strong></code><strong><code>.before</code></strong> 方法可以在<code>ChildNode这个节点的父节点中插入一些列的</code> {{domxref("Node")}} 或者 {{domxref("DOMString")}} 对象,位置就是在<code>ChildNode节点的前面,</code>{{domxref("DOMString")}} 对象其实和 {{domxref("Text")}}节点一样的方式来完成插入的。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">[Throws, Unscopable]
void ChildNode.before((Node or DOMString)... nodes);
</pre>
<h3 id="Parameters参数">Parameters参数</h3>
<dl>
<dt><code>nodes</code></dt>
<dd>一系列的 {{domxref("Node")}} 或者 {{domxref("DOMString")}} </dd>
</dl>
<h3 id="Exceptions">Exceptions</h3>
<ul>
<li>{{domxref("HierarchyRequestError")}}: 当节点插入了错误的层级就会出现报错,需要遵循html标签的层级关系,</li>
</ul>
<h2 id="Examples事例">Examples事例</h2>
<h3 id="Inserting_an_element插入元素节点">Inserting an element插入元素节点</h3>
<pre class="brush: js">var parent = document.createElement("div");
var child = document.createElement("p");
parent.appendChild(child);
var span = document.createElement("span");
child.before(span);
console.log(parent.outerHTML);
// "<div><span></span><p></p></div>"
</pre>
<h3 id="Inserting_text插入文本节点">Inserting text插入文本节点</h3>
<pre class="brush: js">var parent = document.createElement("div");
var child = document.createElement("p");
parent.appendChild(child);
child.before("Text");
console.log(parent.outerHTML);
// "<div>Text<p></p></div>"</pre>
<h3 id="Inserting_an_element_and_text同时插入文本和元素">Inserting an element and text同时插入文本和元素</h3>
<pre class="brush: js">var parent = document.createElement("div");
var child = document.createElement("p");
parent.appendChild(child);
var span = document.createElement("span");
child.before(span, "Text");
console.log(parent.outerHTML);
// "<div><span></span>Text<p></p></div>"</pre>
<h3 id="ChildNode.before()_is_unscopable不可使用区域"><code>ChildNode.before()</code> is unscopable不可使用区域</h3>
<p>The <code>before()</code> 不能配合with声明使用,See {{jsxref("Symbol.unscopables")}} for more information.</p>
<pre class="brush: js">with(node) {
before("foo");
}
// ReferenceError: before is not defined </pre>
<h2 id="Polyfill">Polyfill</h2>
<p>兼容ie9或者更高版本的方法,You can polyfill the <code>before() method</code> in Internet Explorer 9 and higher with the following code:</p>
<pre class="brush: js">// from: https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/before()/before().md
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty('before')) {
return;
}
Object.defineProperty(item, 'before', {
configurable: true,
enumerable: true,
writable: true,
value: function before() {
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.insertBefore(docFrag, this);
}
});
});
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);</pre>
<h2 id="Specification">Specification</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('DOM WHATWG', '#dom-childnode-before', 'ChildNode.before()')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatChrome(54.0)}}</td>
<td>{{CompatGeckoDesktop(49)}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatOpera(39)}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Android Webview</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
<th>Chrome for Android</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatNo}}</td>
<td>{{CompatChrome(54.0)}}</td>
<td>{{CompatGeckoMobile(49)}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatOpera(39)}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatChrome(54.0)}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also">See also</h2>
<ul>
<li>{{domxref("ChildNode")}} and {{domxref("ParentNode")}}</li>
<li>{{domxref("ChildNode.after()")}}</li>
<li>{{domxref("ParentNode.append()")}}</li>
<li>{{domxref("Node.appendChild()")}}</li>
<li>{{domxref("Node.insertBefore()")}}</li>
<li>{{domxref("NodeList")}}</li>
</ul>
|