CharacterData: replaceWith() メソッド
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2018年4月.
replaceWith()
は CharacterData
インターフェイスのメソッドで、親ノードの子リスト内にあるこのノードを、一連の Node
オブジェクトまたは文字列で置き換えます。
構文
js
replaceWith(nodes)
引数
メモ: 引数が渡されなかった場合、このメソッドはこのノードを DOM ツリーから取り除くだけの動作をします。
返値
なし (undefined
)。
例外
HierarchyRequestError
DOMException
-
ノードが階層内の指定された位置に挿入できない場合に発生します。
例
html
<p id="myText">Some text</p>
js
let text = document.getElementById("myText").firstChild;
let em = document.createElement("em");
em.textContent = "Italic text";
text.replaceWith(em); // `Some text` を `Italic text` で置き換える
仕様書
Specification |
---|
DOM> # ref-for-dom-childnode-replacewith①> |
ブラウザーの互換性
Loading…