如何将处理程序绑定到窗口上的选择更改? [英] How to bind a handler to a selection change on window?

查看:69
本文介绍了如何将处理程序绑定到窗口上的选择更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我需要知道 window.getSelection()何时发生了变化,并将处理程序绑定到此事件。想法?

Basically I need to know when the window.getSelection() has changed and bind a handler to this event. Ideas?

OBS:请注意,我不打算在INPUT或TEXTAREA上绑定选择更改。我正在谈论窗口中的任何选择。

OBS: Please note that I'm not looking to bind a selection change on a INPUT or TEXTAREA. I'm talking about any selection in the window.

推荐答案

没有跨浏览器方式来检测选择的更改。 IE(我认为自5.5版本)和WebKit / Blink浏览器(例如,过去几年的Chrome,Safari和Opera版本)支持 selectionchange 事件。 文档它完全符合你的要求。

There is no cross-browser way of detecting changes to the selection. IE (since version 5.5, I think) and WebKit/Blink browsers (Chrome, Safari and Opera version from the last couple of years, for example) support a selectionchange event on the document which does exactly what you want.

Firefox和pre-Blink Opera没有这样的事件你可以do检测通过键盘和鼠标事件进行的选择更改,这是不能令人满意的(例如,无法从上下文或编辑菜单中检测到全选),或者只是轮询选择 返回的对象window.getSelection()(检查选择的 anchorNode anchorOffset focusNode focusOffset 属性与之前的值相对应足够了。)

Firefox and pre-Blink Opera have no such event and all you can do is detect selection changes made via keyboard and mouse events, which is unsatisfactory (there is no way of detecting "Select All" from context or edit menus, for example), or simply poll the Selection object returned by window.getSelection() (checking the selection's anchorNode, anchorOffset, focusNode and focusOffset properties against their previous values should be sufficient).

这篇关于如何将处理程序绑定到窗口上的选择更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆