aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/element/before/index.html
blob: 652f38c95fec0be922d26864530783bb276b1557 (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.before()
slug: Web/API/Element/before
tags:
  - API
  - DOM
  - Method
  - Node
  - Reference
browser-compat: api.Element.before
translation_of: Web/API/Element/before
original_slug: Web/API/ChildNode/before
---
<div>{{APIRef("DOM")}}</div>

<p><code><strong>Element.before()</strong></code> は一連の {{domxref("Node")}} または {{domxref("DOMString")}} オブジェクトを、この <code>Element</code> の親の子リストの中、この <code>Element</code> の直前に挿入します。 {{domxref("DOMString")}} オブジェクトは {{domxref("Text")}} ノードと等価なノードとして挿入されます。</p>

<h2 id="Syntax">構文</h2>

<pre class="brush: js">before(... 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.before(span);

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

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

<pre class="brush: js">let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);

p.before("Text");

console.log(container.outerHTML);
// "&lt;div&gt;Text&lt;p&gt;&lt;/p&gt;&lt;/div&gt;"</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.before(span, "Text");

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


<h2 id="Specification">仕様書</h2>

{{Specifications}}

<h2 id="Browser_compatibility">ブラウザーの互換性</h2>

<p>{{Compat}}</p>

<h2 id="See_also">関連情報</h2>

<ul>
  <li>{{domxref("Element.after()")}}</li>
  <li>{{domxref("Element.append()")}}</li>
  <li>{{domxref("Node.appendChild()")}}</li>
  <li>{{domxref("Node.insertBefore()")}}</li>
  <li>{{domxref("Element.insertAdjacentElement()")}}</li>
  <li>{{domxref("NodeList")}}</li>
</ul>