Document: selectionchange event
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.
Sự kiện selectionchange của Selection API được kích hoạt khi Selection hiện tại của một Document thay đổi.
Sự kiện này không thể hủy và không nổi bọt.
Sự kiện có thể được xử lý bằng cách thêm trình lắng nghe sự kiện cho selectionchange hoặc sử dụng trình xử lý sự kiện onselectionchange.
Note:
Sự kiện này không hoàn toàn giống như sự kiện selectionchange được kích hoạt khi vùng chọn văn bản trong phần tử <input> hoặc <textarea> thay đổi. Xem sự kiện addEventListener(), hoặc đặt một thuộc tính trình xử lý sự kiện.
js
addEventListener("selectionchange", (event) => { })
onselectionchange = (event) => { }
Loại sự kiện
Một Event chung.
Ví dụ
js
// Phiên bản addEventListener
document.addEventListener("selectionchange", () => {
console.log(document.getSelection());
});
// Phiên bản onselectionchange
document.onselectionchange = () => {
console.log(document.getSelection());
};
Đặc tả kỹ thuật
| Specification |
|---|
| Selection API> # selectionchange-event> |
| Selection API> # dom-globaleventhandlers-onselectionchange> |