ChildNode.remove()

ChildNode.remove() 方法,把对象从它所属的 DOM 树中删除。

语法

node.remove();

示例

使用 remove()

<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>
var el = document.getElementById('div-02');
el.remove();
// id 为 'div-02' 的 div 被删掉了

ChildNode.remove() 是不可见的

with 语句中,remove() 方法是不可见的。参阅 Symbol.unscopables 了解更多信息。

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

Polyfill

You can polyfill the remove() method in Internet Explorer 9 and higher with the following code:

//https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md
(function (arr) {
   arr.forEach(function (item) {
    if (item.hasOwnProperty('remove')) {
      return;
    }
    Object.defineProperty(item, 'remove', {
      configurable: true,
      enumerable: true,
      writable: true,
      value: function remove() {
        this.parentNode.removeChild(this);
      }
    });
  });
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);

规范

规范 状态 注释
DOM
ChildNode.remove
Living Standard Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
removeChrome Full support 23Edge Full support 12Firefox Full support 23IE No support NoOpera Full support 15Safari Full support 7WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 23Opera Android Full support 14Safari iOS Full support 7Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support

参见