ChildNode.remove()
method removes the object from the tree it belongs to.
node.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-02');
el.remove(); // Removes the div with the 'div-02' id
ChildNode.remove()
is unscopable
remove()
method is not scoped into the
with
statement. See
Symbol.unscopables
了解更多信息。
with(node) {
remove();
}
// ReferenceError: remove is not defined
You can polyfill the
remove()
method in Internet Explorer 9 and higher with the following code:
// 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]);
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'ChildNode.remove' in that specification. |
实时标准 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
remove
|
Chrome 23 | Edge 12 | Firefox 23 | IE No | Opera 15 | Safari 7 | WebView Android ≤37 | Chrome Android 25 | Firefox Android 23 | Opera Android 14 | Safari iOS 7 | Samsung Internet Android 1.5 |
完整支持
不支持
ChildNode
纯接口。
CharacterData
,
元素
,和
DocumentType
.
ChildNode
after()
before()
remove()
replaceWith()
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument