ChildNode.remove() method removes the object from the tree it belongs to.

句法

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(); // Removes the div with the 'div-02' id
					

ChildNode.remove() is unscopable

remove() method is not scoped into the with statement. See 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:

// from: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
The definition of 'ChildNode.remove' in that specification.
实时标准 初始定义。

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
remove Chrome 23 Edge 12 Firefox 23 IE No Opera 15 Safari 7 WebView Android ≤37 Chrome Android 25 Firefox Android 23 Opera Android 14 Safari iOS 7 Samsung Internet Android 1.5

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: