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

浏览器兼容性

另见