O método ChildNode.remove()
remove o objeto da árvore a que ele pertence.
Sintase
elementNodeReference.remove();
Exemplo
Usando 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(); // Remove o div com o id 'div-02'
ChildNode.remove()
não tem escopo
O método remove()
não tem escopo na função with
. Veja Symbol.unscopables
para mais informação.
with(node) {
remove();
}
// ReferenceError: remove is not defined
Polyfill
Você pode evitar incompatibilidade ao usar o método remove() no
Internet Explorer 9 em diante com o seguinte código:
// de: 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]);
Especificações
Especificação | Situação | Comentário |
---|---|---|
DOM The definition of 'ChildNode.remove' in that specification. |
Padrão em tempo real | Definição Inicial. |
DOM4 The definition of 'ChildNode.remove' in that specification. |
Obsoleto |
Compatibilidade de navegadores
BCD tables only load in the browser
Veja também
- A interface pura
ChildNode
.