Element.replaceWith()
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.
语法
js
replaceWith(param1)
replaceWith(param1, param2)
replaceWith(param1, param2, /* …, */ paramN)
参数
异常
HierarchyRequestError
DOMException
-
无法在层次结构中的指定节点处插入节点时抛出。
示例
使用 replaceWith()
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()
是非作用域方法
replaceWith()
方法不能作用于 with 语句。参考 Symbol.unscopables
获取更多信息。
js
with (node) {
replaceWith("foo");
}
// ReferenceError: replaceWith is not defined
规范
Specification |
---|
DOM Standard # ref-for-dom-childnode-replacewith① |
浏览器兼容性
BCD tables only load in the browser