DOM
The Document.replaceChildren() method replaces the
existing children of a Document with a specified new set of children.
Syntax
replaceChildren(param1)
replaceChildren(param1, param2)
replaceChildren(param1, param2, /* …, */ paramN)
Parameters
param1, …,paramN- : A set of Node objects or strings to replace the
Document's existing children with. If no replacement objects are specified, then theDocumentis emptied of all child nodes.
- : A set of Node objects or strings to replace the
Return value
None (undefined).
Exceptions
HierarchyRequestErrorDOMException- : Thrown if the constraints of the node tree are violated.
Examples
Emptying a document
replaceChildren() provides a very convenient mechanism for emptying a document
of all its children. You call it on the document without any argument specified:
document.replaceChildren();
document.children; // HTMLCollection []
Specifications
Browser compatibility
See also
- Document.prepend()
- Document.append()
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
