Metoda ChildNode.remove()
usuwa obiekt z drzewa, do którego należy.
Składnia
node.remove();
Przykład
Użycie remove()
<div id="div-01">To jest div-01</div>
<div id="div-02">To jest div-02</div>
<div id="div-03">To jest div-03</div>
var el = document.getElementById('div-02');
el.remove(); // Usuwa div z ID 'div-02'
ChildNode.remove()
jest poza zakresem
Metoda remove()
nie wchodzi w zakres wyrażenia with
. Zobacz Symbol.unscopables
, aby uzyskać więcej informacji.
with(node) {
remove();
}
// ReferenceError: remove is not defined
Polyfill
Można utworzyć polyfill metody remove()
w Internet Explorer 9 (lub wyższej wersji) za pomocą następującego kodu:
// 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]);
Specyfikacje
Specification | Status | Comment |
---|---|---|
DOM The definition of 'ChildNode.remove' in that specification. |
Living Standard | Initial definition. |
DOM4 The definition of 'ChildNode.remove' in that specification. |
Obsolete |
Kompatybilność z przeglądarkami
BCD tables only load in the browser
Zobacz również
- The
ChildNode
pure interface.