Element: remove() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

Element.remove() 方法将元素从其父节点中移除。如果该元素没有父节点,则调用 remove() 方法不会执行任何操作。

语法

js
remove()

参数

无。

返回值

无(undefined)。

示例

使用 remove()

html
<div id="div-01">Here is div-01</div>
<div id="div-02">Here is div-02</div>
<div id="div-03">Here is div-03</div>
js
const element = document.getElementById("div-02");
element.remove(); // Removes the div with the 'div-02' id

Element.remove() 是不可作用域的

remove() 方法不能在 with 语句的作用域内使用。有关更多信息,请参阅 Symbol.unscopables

js
with (node) {
  remove();
}
// ReferenceError: remove is not defined

规范

规范
DOM
# ref-for-dom-childnode-remove①

浏览器兼容性

另见