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)
参数
param1
、…、paramN
-
一系列用来替换的
Node
对象或者字符串对象。
异常
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 # ref-for-dom-childnode-replacewith① |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
replaceWith |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.