Node.removeChild()
method removes a child node from the DOM and returns the removed node.
var oldChild = node.removeChild(child); OR node.removeChild(child);
child
is the child node to be removed from the DOM.
node
is the parent node of
child
.
oldChild
holds a reference to the removed child node, i.e.,
oldChild === child
.
The removed child node still exists in memory, but is no longer part of the DOM. With the first syntax form shown, you may reuse the removed node later in your code, via the
oldChild
object reference.
In the second syntax form, however, there is no
oldChild
reference kept, so assuming your code has not kept any other reference to the node elsewhere, it will immediately become unusable and irretrievable, and will usually be
automatically deleted
from memory after a short time.
若
child
is actually not a child of the
element
node, the method throws an exception. This will also happen if
child
was in fact a child of
element
at the time of the call, but was removed by an event handler invoked in the course of trying to remove the element (e.g.,
blur
)。
The method throws an exception in 2 different ways:
若
child
was in fact a child of
element
and so existing on the DOM, but was removed the method throws the following exception:
Uncaught NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node
.
若
child
doesn't exist on the DOM of the page, the method throws the following exception:
Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'.
Given this HTML:
<div id="top"> <div id="nested"></div> </div>
To remove a specified element when knowing its parent node:
let d = document.getElementById("top");
let d_nested = document.getElementById("nested");
let throwawayNode = d.removeChild(d_nested);
To remove a specified element without having to specify its parent node:
let node = document.getElementById("nested");
if (node.parentNode) {
node.parentNode.removeChild(node);
}
To remove all children from an element:
let element = document.getElementById("top");
while (element.firstChild) {
element.removeChild(element.firstChild);
}
<!--Sample HTML code-->
<div id="top"> </div>
<script type="text/javascript">
let top = document.getElementById("top");
let nested = document.getElementById("nested");
// Throws Uncaught TypeError
let garbage = top.removeChild(nested);
</script>
<!--Sample HTML code-->
<div id="top">
<div id="nested"></div>
</div>
<script type="text/javascript">
let top = document.getElementById("top");
let nested = document.getElementById("nested");
// This first call correctly removes the node
let garbage = top.removeChild(nested);
// Throws Uncaught NotFoundError
garbage = top.removeChild(nested);
</script>
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'Node: removeChild' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
removeChild
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5 | Opera 7 | Safari 1.1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
完整支持
节点
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
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