Selection:selectAllChildren() 方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

Selection.selectAllChildren() 方法將指定節點的全部子節點新增到選區。之前的選區將被移除。

語法

js
selectAllChildren(parentNode)

引數

parentNode

parentNode 的所有子節點都將被選中。parentNode 本身不包含在選區內。

返回值

無(undefined)。

示例

HTML

html
<main>
  <button>Select Footer</button>
  <p>Welcome to my website.</p>
  <p>I hope you enjoy your visit.</p>
</main>
<footer>
  <address>webmaster@example.com</address>
  <p>© 2019</p>
</footer>

JavaScript

js
const button = document.querySelector("button");
const footer = document.querySelector("footer");

button.addEventListener("click", (e) => {
  window.getSelection().selectAllChildren(footer);
});

結果

規範

規範
Selection API
# dom-selection-selectallchildren

瀏覽器相容性

另見