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①

瀏覽器相容性

另見