这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
ParentNode.replaceChildren()
method replaces the existing children of a
节点
with a specified new set of children. These can be
DOMString
or
节点
对象。
// [Throws, Unscopable] ParentNode.replaceChildren(...nodesOrDOMStrings) // returns undefined
nodesOrDOMStrings
节点
or
DOMString
objects to replace the
ParentNode
's existing children with. If no replacement objects are specified, then the
ParentNode
is emptied of all child nodes.
HierarchyRequestError
:
constraints of the node tree
are violated.
replaceChildren()
provides a very convenient mechanism for emptying a node of all its children. You simply call it on the parent node without any argument specified:
myNode.replaceChildren();
replaceChildren()
enables you to easily transfer nodes between parents, without having to resort to verbose looping code. For example, say we have a simple application allowing you to choose what food you want for your party. This HTML might look something like this:
<h2>Party food option list</h2>
<main>
<div>
<label for="no">No thanks!</label>
<select id="no" multiple size="10">
<option>Apples</option>
<option>Oranges</option>
<option>Grapes</option>
<option>Bananas</option>
<option>Kiwi fruits</option>
<option>Chocolate cookies</option>
<option>Peanut cookies</option>
<option>Chocolate bars</option>
<option>Ham Sandwiches</option>
<option>Cheese Sandwiches</option>
<option>Falafel sandwiches</option>
<option>Ice cream</option>
<option>Jelly</option>
<option>Carrot sticks and houmous</option>
<option>Margherita pizza</option>
<option>Pepperoni pizza</option>
<option>Vegan veggie pizza</option>
</select>
</div>
<div class="buttons">
<button id="to-yes">Transfer to "Yes" --></button>
<button id="to-no"><-- Transfer to "No"</button>
</div>
<div>
<label for="yes">Yes please!</label>
<select id="yes" multiple size="10">
</select>
</div>
</main>
It would make sense to use some simple CSS to lay out the two select lists in a line alongside one another, with the control buttons in between them:
main {
display: flex;
}
div {
margin-right: 20px;
}
label, button {
display: block;
}
.buttons {
display: flex;
flex-flow: column;
justify-content: center;
}
select {
width: 200px;
}
What we want to do is transfer any selected options in the "no" list over to the "yes" list when the "yes" button is pressed, and transfer any selected options in the "yes" list over to the "no" list when the "no" button is pressed.
To do this, we give each of the buttons a click event handler, which collects together the selected options you want to transfer in one constant, and the existing options in the list you are transferring to in another constant. It then calls
replaceChildren()
on the list to transfer the options to, using the spread operator to pass in all the options contained in both constants.
const noSelect = document.getElementById('no');
const yesSelect = document.getElementById('yes');
const noBtn = document.getElementById('to-no');
const yesBtn = document.getElementById('to-yes');
yesBtn.addEventListener('click', () => {
const selectedTransferOptions = document.querySelectorAll('#no option:checked');
const existingYesOptions = document.querySelectorAll('#yes option');
yesSelect.replaceChildren(...selectedTransferOptions, ...existingYesOptions);
});
noBtn.addEventListener('click', () => {
const selectedTransferOptions = document.querySelectorAll('#yes option:checked');
const existingNoOptions = document.querySelectorAll('#no option');
noSelect.replaceChildren(...selectedTransferOptions, ...existingNoOptions);
});
The end result looks like this:
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'ParentNode.replaceChildren()' in that specification. |
实时标准 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
replaceChildren
|
Chrome No | Edge No | Firefox 78 | IE No | Opera No | Safari 14 | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS 14 | Samsung Internet Android No |
完整支持
不支持
实验。期望将来行为有所改变。
ParentNode
append()
prepend()
querySelector()
querySelectorAll()
replaceChildren()
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