aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/childnode/before/index.html
blob: 109db0d9e4ae464d2a1135b5c56769c186f57141 (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
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
  - Method
  - Node
  - Reference
translation_of: Web/API/ChildNode/before
---
<div>{{APIRef("DOM")}}</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="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox notranslate">[Throws, Unscopable]
void ChildNode.before((Node or DOMString)... nodes);
</pre>

<h3 id="Parameters" name="Parameters">パラメーター</h3>

<dl>
 <dt><code>nodes</code></dt>
 <dd>{{domxref("Node")}} または {{domxref("DOMString")}} オブジェクトのセットを挿入します。</dd>
</dl>

<h3 id="Exceptions" name="Exceptions">例外</h3>

<ul>
 <li>{{domxref("HierarchyRequestError")}}: ノードは階層の指定の位置には挿入できません。</li>
</ul>

<h2 id="Examples" name="Examples"></h2>

<h3 id="Inserting_an_element" name="Inserting_an_element">要素の挿入</h3>

<pre class="brush: js notranslate">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);
// "&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;"
</pre>

<h3 id="Inserting_text" name="Inserting_text">テキストの挿入</h3>

<pre class="brush: js notranslate">var parent = document.createElement("div");
var child = document.createElement("p");
parent.appendChild(child);

child.before("Text");

console.log(parent.outerHTML);
// "&lt;div&gt;Text&lt;p&gt;&lt;/p&gt;&lt;/div&gt;"</pre>

<h3 id="Inserting_an_element_and_text" name="Inserting_an_element_and_text">要素とテキストの挿入</h3>

<pre class="brush: js notranslate">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);
// "&lt;div&gt;&lt;span&gt;&lt;/span&gt;Text&lt;p&gt;&lt;/p&gt;&lt;/div&gt;"</pre>

<h3 id="ChildNode.before_is_unscopable" name="ChildNode.before_is_unscopable"><code>ChildNode.before()</code> はスコーピングに非対応</h3>

<p><code>before()</code> メソッドは <code>with</code> 文でのスコーピングに対応していません。詳細は {{jsxref("Symbol.unscopables")}} をご覧ください。</p>

<pre class="brush: js notranslate">with(node) {
  before("foo");
}
// ReferenceError: before is not defined </pre>

<h2 id="Polyfill" name="Polyfill">ポリフィル</h2>

<p>以下のポリフィルで、 Internet Explorer 9 以降でも <code>before()</code> メソッドが利用できます。</p>

<pre class="brush: js notranslate">// 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" name="Specification">仕様</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="Browser_compatibility" name="Browser_compatibility">ブラウザー実装状況</h2>



<p>{{Compat("api.ChildNode.before")}}</p>

<h2 id="See_also" name="See_also">関連情報</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("Element.insertAdjacentElement()")}}</li>
 <li>{{domxref("NodeList")}}</li>
</ul>