--- title: Element.replaceWith() slug: Web/API/Element/replaceWith tags: - API - DOM - Method - Element - Reference browser-compat: api.Element.replaceWith translation_of: Web/API/Element/replaceWith original_slug: Web/API/ChildNode/replaceWith ---
Element.replaceWith() メソッドは、この Element を親の子リストの中で一連の {{domxref("Node")}} または {{domxref("DOMString")}} オブジェクトに置換します。 {{domxref("DOMString")}} オブジェクトは {{domxref("Text")}} ノードと等価なノードとして挿入されます。
replaceWith(...nodes)
nodesreplaceWith() の使用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() はスコーピングに非対応replaceWith() メソッドは with 文でのスコーピングに対応していません。詳細は {{jsxref("Symbol.unscopables")}} をご覧ください。
with(node) {
replaceWith("foo");
}
// ReferenceError: replaceWith is not defined
{{Specifications}}
{{Compat}}