HTMLTextAreaElement: select event
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.
Sự kiện select được kích hoạt khi một phần văn bản đã được chọn.
Cú pháp
Dùng tên sự kiện trong các phương thức như addEventListener(), hoặc đặt thuộc tính trình xử lý sự kiện.
js
addEventListener("select", (event) => { })
onselect = (event) => { }
Loại sự kiện
Một Event chung.
Ví dụ
>Trình ghi lại vùng chọn
html
<textarea>Try selecting some text in this element.</textarea>
<p id="log"></p>
js
function logSelection(event) {
const log = document.getElementById("log");
const selection = event.target.value.substring(
event.target.selectionStart,
event.target.selectionEnd,
);
log.textContent = `You selected: ${selection}`;
}
const textarea = document.querySelector("textarea");
textarea.addEventListener("select", logSelection);
Tương đương onselect
Bạn cũng có thể thiết lập trình xử lý sự kiện bằng thuộc tính onselect:
js
textarea.onselect = logSelection;
Đặc tả kỹ thuật
| Thông số kỹ thuật |
|---|
| HTML> # event-select> |
| HTML> # handler-onselect> |