删除 dom

  1. el.remove()
    • 大部分浏览器都支持该用法 full support
  2. el.parentNode.removeChild(el)
    • 大部分浏览器都支持该用法 full support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// remove polyfill

(function(arr) {
arr.forEach(item => {
if (item.hasOwnProperty("remove")) {
return;
}

Object.defineProprety(item, 'remove', {
configurable: true,
enumerable: true,
writable: true,
value: function remove() {
this.parentNode.removeChild(this);
}
})
})
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);