Element: before() メソッド
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Element.before()
は一連の Node
オブジェクトまたは文字列を、この Element
の親の子リストの中、この Element
の直前に挿入します。文字列は Text
ノードと等価なノードとして挿入されます。
構文
js
before(param1)
before(param1, param2)
before(param1, param2, /* … ,*/ paramN)
引数
param1
, …,paramN
-
挿入する一連の
Node
オブジェクトまたは文字列です。
返値
なし (undefined
)。
例外
HierarchyRequestError
DOMException
-
ノードが階層構造の中の指定された位置に挿入できなかったときに発生します。
例
要素の挿入
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);
// "<div><span></span><p></p></div>"
テキストの挿入
js
let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
p.before("Text");
console.log(container.outerHTML);
// "<div>Text<p></p></div>"
要素とテキストの挿入
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);
// "<div><span></span>Text<p></p></div>"
仕様書
Specification |
---|
DOM # ref-for-dom-childnode-before① |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
before |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.