CharacterData: 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.

before()CharacterData インターフェイスのメソッドで、一連の Node オブジェクトまたは文字列を、この CharacterData の親ノードの子リストの中、この CharacterData ノードの直前に挿入します。

文字列は Text ノードとして挿入されます。文字列は Text() コンストラクターの引数として挿入されます。

構文

js
before(...nodes)

引数

nodes

挿入する一連の Node または文字列です。

例外

HierarchyRequestError DOMException

階層内の指定された位置に新しいノードを挿入できない場合,つまり以下の条件のいずれかに該当する場合に発生します。

before() メソッドを使うと、現在のノードのデータを変更せずに、新しいノードをこの CharacterData ノードの前に挿入することができます。

js
const h1TextNode = document.querySelector("h1").firstChild;
h1TextNode.before("h1# ");

h1TextNode.parentElement.childNodes;
// NodeList [#text "h1# ", #text "CharacterData.before()"]

h1TextNode.data;
// "CharacterData.before()"

仕様書

Specification
DOM
# ref-for-dom-childnode-before①

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
before

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報