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
|
---
title: ChildNode.before()
slug: Web/API/ChildNode/before
tags:
- API
- DOM
- 노드
- 레퍼런스
- 메소드
- 실험중
translation_of: Web/API/ChildNode/before
---
<div>{{APIRef("DOM")}} {{SeeCompatTable}}</div>
<p><code><strong>ChildNode.before</strong></code> 메소드는 <code>ChildNode</code> 의 부모가 가진 자식의 <code>ChildNode</code> 바로 이전에 {{domxref("Node")}} 또는 {{domxref("DOMString")}} 객체의 집합을 삽입합니다.{{domxref("DOMString")}} 객체는 {{domxref("Text")}} 노드와 동일하게 삽입됩니다.</p>
<h2 id="문법">문법</h2>
<pre class="syntaxbox">[Throws, Unscopable]
void ChildNode.before((Node or DOMString)... nodes);
</pre>
<h3 id="파라미터">파라미터</h3>
<dl>
<dt><code>nodes</code></dt>
<dd>삽입할 {{domxref("Node")}} 또는 {{domxref("DOMString")}} 객체의 집합입니다.</dd>
</dl>
<h3 id="예외">예외</h3>
<ul>
<li>{{domxref("HierarchyRequestError")}}: 노드는 계층 구조의 특정 지점에 삽입될 수 없습니다.</li>
</ul>
<h2 id="예제">예제</h2>
<h3 id="엘리먼트_삽입하기">엘리먼트 삽입하기</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="텍스트_삽입하기">텍스트 삽입하기</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="엘리먼트와_텍스트_삽입하기">엘리먼트와 텍스트 삽입하기</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()_는_범위를_지정할_수_없습니다"><code>ChildNode.before()</code> 는 범위를 지정할 수 없습니다</h3>
<p><code>before()</code> 메소드는 <code>with</code> 구문으로 범위를 지정할 수 없습니다. 자세한 내용은 {{jsxref("Symbol.unscopables")}} 문서를 확인하세요.</p>
<pre class="brush: js">with(node) {
before("foo");
}
// ReferenceError: before is not defined </pre>
<h2 id="폴리필">폴리필</h2>
<p>다음 코드를 사용해 인터넷 익스플로러 9 이상에서 <code>before() 메소드</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="명세">명세</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">명세</th>
<th scope="col">상태</th>
<th scope="col">코멘트</th>
</tr>
<tr>
<td>{{SpecName('DOM WHATWG', '#dom-childnode-before', 'ChildNode.before()')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td>초기 정의.</td>
</tr>
</tbody>
</table>
<h2 id="브라우저_호환성">브라우저 호환성</h2>
<p>{{Compat("api.ChildNode.before")}}</p>
<h2 id="함께_보기">함께 보기</h2>
<ul>
<li>{{domxref("ChildNode")}} 와 {{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>
|