Movatterモバイル変換


[0]ホーム

URL:


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

Element: 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⁩.

TheElement.replaceWith() method replaces thisElement in the children list of its parent with a set ofNode objects or strings. Strings are inserted as equivalentText nodes.

Syntax

js
replaceWith(param1)replaceWith(param1, param2)replaceWith(param1, param2, /* …, */ paramN)

Parameters

param1, …,paramN

A set ofNode objects or strings to replace.

Return value

None (undefined).

Exceptions

HierarchyRequestErrorDOMException

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

Examples

UsingreplaceWith()

js
const div = document.createElement("div");const p = document.createElement("p");div.appendChild(p);const span = document.createElement("span");p.replaceWith(span);console.log(div.outerHTML);// "<div><span></span></div>"

replaceWith() is unscopable

ThereplaceWith() method is not scoped into thewithstatement. SeeSymbol.unscopables for more information.

js
with (node) {  replaceWith("foo");}// ReferenceError: replaceWith is not defined

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