我們的志工尚未將本文翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!
您也可以閱讀本文的 English (US) 版本。

The select event fires when some text has been selected.

The event is not available for all elements in all languages. For example, in HTML, select events can be dispatched only on form <input type="text"> and <textarea> elements.

General info

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

Properties

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

Example

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

Result

Specifications

Specification Status Comment
UI Events
The definition of 'select' in that specification.
Working Draft  

See also

文件標籤與貢獻者

最近更新: mfluehr,