We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

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

语法

elementNodeReference.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-01');

el.nextElementSibling.remove();
// id 为 'div-02' 的 div 被删掉了

ChildNode.remove() 是不可见的

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

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

Polyfill

在Internet Explorer 9及更高版本中, 你可以填充 remove() 方法,使用以下代码:

//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.
DOM4
ChildNode.remove
Obsolete  

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

特征 Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (on Element) 23.0 23.0 (23.0) 未实现 10.0 7.0
Support on CharacterData and DocumentType 29.0 23.0 (23.0) 未实现 16.0 7.0
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (on Element) (Yes) 23.0 (23.0) 未实现 10.0 未实现
Support on CharacterData and DocumentType (Yes) 23.0 (23.0) 未实现 16.0 未实现

相关链接

文档标签和贡献者

标签: 
最后编辑者: pasturn,