HTMLInputElement: sự kiện select
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 kích hoạt khi một số văn bản đã được chọn.
Cú pháp
Sử 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ụ
>Ghi nhật ký lựa chọn
html
<input value="Thử chọn một số văn bản trong phần tử này." />
<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 = `Bạn đã chọn: ${selection}`;
}
const input = document.querySelector("input");
input.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
input.onselect = logSelection;
Đặc tả kỹ thuật
| Specification |
|---|
| HTML> # event-select> |
| HTML> # handler-onselect> |