--- title: selectionchange slug: Web/API/Document/selectionchange_event translation_of: Web/API/Document/selectionchange_event ---
Selection API 的 selectionchange 事件在文档上的当前文本选择被改变时触发。
| Bubbles | No |
| Cancelable | No |
| Target objects | {{domxref("Document")}} |
| Interface | {{domxref("Event")}} |
//以下两种方法可任选其一
document.addEventListener("selectionchange", () => {
console.log(document.getSelection());
});
document.onselectionchange = () => {
console.log(document.getSelection());
};
selectionchange 事件实现了 {{domxref("Event")}} 接口,因此在此接口上定义的属性和方法都可使用。
{{InheritanceDiagram('','','', 'Event')}}
| 规范 | 状态 | 说明 |
|---|---|---|
| {{SpecName('Selection API', '#selectionchange-event', 'selectionchange')}} | {{Spec2('Selection API')}} | Initial definition. |
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoDesktop("52")}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} |
on HTMLInputElement and HTMLTextAreaElement |
{{CompatNo}} | {{CompatNo}} | {{CompatGeckoDesktop("52")}} - [1] | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
| Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|---|
| Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile("52")}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} |
on HTMLInputElement and HTMLTextAreaElement |
{{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatGeckoMobile("52")}} - [1] | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
[1] - Experimental support in Nightly behind the dom.select_events.textcontrols.enabled preference. Support for input and textarea is currently not part of the specification.