获取鼠标指针下的文本 [英] Getting the text under the mouse pointer
问题描述
我正在编写一个浏览器插件,想要通过Javascript获取用户当前悬停的单词/单词。
I'm writing a browser add-on and want to get the word / words the user is currently hovering over via Javascript.
推荐答案
一种选择是将每个单词放在自己的< span>
元素中,并添加 mouseover
处理程序到检查事件的目标
/ srcElement
属性的主体,以检索范围,从而检索单词。这有很大的缺点:用跨度围绕每个单词的初始过程可能很慢;新的跨度可能会破坏现有的CSS规则;该文档最终将包含大量没有语义价值的元素。
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.
这篇关于获取鼠标指针下的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!