Die ChildNode.remove()
Methode entfernt ein Objekt aus der Baumstruktur ("tree") zu der es gehört.
Syntax
node.remove();
Beispiel
Benutzung von remove()
<div id="div-01">Dies ist div-01</div>
<div id="div-02">Dies ist div-02</div>
<div id="div-03">Dies ist div-03</div>
var el = document.getElementById('div-02');
el.remove(); // Entfernt das div Element mit der id 'div-02'
ChildNode.remove() kann nicht gescopet werden
Die remove()
Methode kann nicht mit dem with
Statement gescopet werden. Symbol.unscopables
enthält mehr Informationen darüber.
with(node) {
remove();
}
// Erzeught einen ReferenceError
Polyfill
Ein Polyfill der remove()
Methode in Internet Explorer 9 und höher sieht folgendermaßen aus:
// von: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() {
if (this.parentNode !== null)
this.parentNode.removeChild(this);
}
});
});
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
Specifikationen
Specifikation | Status | Kommentar |
---|---|---|
DOM Die Definition von 'ChildNode.remove' in dieser Spezifikation. |
Lebender Standard | Erste Definition. |
Browser compatibility
BCD tables only load in the browser
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.
Siehe auch
- Das reine
ChildNode
Interface.