1. 面向开发者的 Web 技术
  2. Web API
  3. HTMLInputElement
  4. select

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

select

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

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

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

General info

Interface UIEvent if generated from a user interface, Event otherwise
Bubbles Yes
Cancelable No
Target Element
Default Action None

属性

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

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

JavaScript

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

结果

规范

规范
HTML
# event-select
HTML
# handler-onselect

参见

帮助改进 MDN

了解如何参与贡献

此页面最后更新于 ,由 MDN 贡献者更新。

AltStyle によって変換されたページ (->オリジナル) /