语法
在诸如 addEventListener() 之类的方法中使用事件名称,或设置事件处理程序属性。
jsaddEventListener("select", (event) => { })
onselect = (event) => { }
事件类型
一个通用的 Event。
示例
选择记录器
html
jsfunction 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 属性来设置事件处理程序。
jsinput.onselect = logSelection;
规范
规范
HTML# event-select
HTML# handler-onselect
浏览器兼容性
加载中…
帮助改进 MDN
此页面对您有帮助吗?
是
否
了解如何贡献 此页面最后修改于 2025年9月25日,由 MDN 贡献者 修改。
在 GitHub 上查看此页面 • 报告此内容的错误