Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. CharacterData
  4. replaceWith()

CharacterData: replaceWith() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨April 2018⁩.

ThereplaceWith() method of theCharacterData interfacereplaces this node in the children list of its parentwith a set ofNode objects or string.

Strings are inserted asText nodes; the string is being passed as argument to theText() constructor.

Syntax

js
replaceWith(...nodes)

Parameters

nodesOptional

A comma-separated list ofNode objects or strings that will replace the current node.

Note:If no arguments are passed in, this method removes the node from the DOM tree.

Return value

None (undefined).

Exceptions

HierarchyRequestErrorDOMException

Thrown when the node cannot be inserted at the specified point in the hierarchy.

Examples

html
<p>Some text</p>
js
let text = document.getElementById("myText").firstChild;let em = document.createElement("em");em.textContent = "Italic text";text.replaceWith(em); // Replace `Some text` by `Italic text`

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp