HTMLInputElement: select 事件
當有文字被選中時,會觸發 select 事件。
語法
在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。
js
addEventListener("select", (event) => { })
onselect = (event) => { }
事件型別
一個通用的 Event。
示例
選擇記錄器
html
<input value="Try selecting some text in this element." />
<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 input = document.querySelector("input");
input.addEventListener("select", logSelection);
onselect 等效項
您也可以使用 onselect 屬性來設定事件處理程式。
js
input.onselect = logSelection;
規範
| 規範 |
|---|
| HTML # event-select |
| HTML # handler-onselect |
瀏覽器相容性
載入中…