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。离线整理了版式、链接和媒体显示。
