这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

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.

异常

范例

Emptying a node

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();
					

Transferring nodes between parents

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.
实时标准 初始定义。

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
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

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改: