Element: remove() 方法
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① |
瀏覽器相容性
載入中…