开发者

Getting the text under the mouse pointer

I'm writing a 开发者_开发知识库browser add-on and want to get the word / words the user is currently hovering over via Javascript.


There's no good general purpose function to do that. If you specify which browser you're adding on to, there might be a specific workaround. When faced with this problem in the past, I had to resort to asking the user to double-click the word (then you can detect the double-click, get selection, and reset it back).


One option is to place every word inside its own <span> element and adding a mouseover handler to the body that checks the event's target / srcElement property to retrieve the span and therefore the word. This has significant downsides: the initial process of surrounding each word with a span could be slow; the new spans could mess up existing CSS rules; the document would end up with loads of elements that have no semantic value.


My guess is that you cannot get this done unless every word is in its own container like etc and you have declared a onmouseover handler for this. An easier approach would be use the currentSelection element when a user double clicks or mouse selects a piece of text on the browser. You could write on mouse click/doubleclick events for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜