大网数据广告活动
Web 标准与前端开发

select

· 52CE 我爱测

该事件不适用于所有语言的所有元素。例如,在 HTML,`select`事件只能在表单input/text和 textarea元素上触发。

select 选择某些文本时会触发事件。

该事件不适用于所有语言的所有元素。例如,在 HTML,select事件只能在表单input/text和 textarea元素上触发。

General info

<table class="properties"> <thead></thead> <tbody> <tr> <th>Interface</th> <td> UIEvent if generated from a user interface, Event otherwise </td> </tr> <tr> <th>Bubbles</th> <td>Yes</td> </tr> <tr> <th>Cancelable</th> <td>No</td> </tr> <tr> <th>Target</th> <td>Element</td> </tr> <tr> <th>Default Action</th> <td>None</td> </tr> </tbody> </table>

属性

Property Type Description
target EventTarget The event target (the topmost target in the DOM tree).
type DOMString The type of event.
bubbles Boolean Whether the event normally bubbles or not.
cancelable Boolean Whether the event is cancellable or not.
view WindowProxy document.defaultView (window of the document)
detail long (float) 0.

示例

HTML

<input value="Try selecting some text in this element." />
<p id="log"></p>

JavaScript

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);

结果

示例

规范

参见

  • GlobalEventHandlers.onselect

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。