Selection: phương thức selectAllChildren()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Phương thức Selection.selectAllChildren() thêm tất cả các phần tử con của nút đã chỉ định vào vùng chọn. Vùng chọn trước đó sẽ bị mất.

Cú pháp

js
selectAllChildren(parentNode)

Tham số

parentNode

Tất cả các phần tử con của parentNode sẽ được chọn. Bản thân parentNode không thuộc vùng chọn.

Giá trị trả về

Không có (undefined).

Ví dụ

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

Kết quả

Thông số kỹ thuật

Specification
Selection API
# dom-selection-selectallchildren

Tương thích trình duyệt

Xem thêm

  • Selection, giao diện mà nó thuộc về.