开发者

How can I highlight every single word in jquery so that I can provide a definition for each word?

开发者 https://www.devze.com 2023-01-11 08:11 出处:网络
What I am trying to do is allow the user to scroll over any word on a web page and when the mouse goes over the word it is highlighted (I know I can use hover for that).However I do not know how to se

What I am trying to do is allow the user to scroll over any word on a web page and when the mouse goes over the word it is highlighted (I know I can use hover for that). However I do not know how to select individual words, from the paragraph. The ult开发者_如何学运维imate goal is to allow the user to click on the word and a definition appears over the word, but I think I can figure that out once I can hover over individual words.


There is no way in Javascript to do that, the smallest thing you can interact with, in general, is a DOM element. So you could technically wrap each word inside a <span>, but that would seem to be seriously overkill and probably have a huge performance impact as well.

Instead, what most sites do, that provide this functionality, is they do the hover popup thing whenever you select text (see this question for some code to get the current selection).

Personally, I find it really irritating (nytimes.com does it like that, for example), but maybe that's just me...


If you want something similar to what the NYtimes provides, I suggest that you use the Select Link plugin in JQuery, which provides a search for any word that is selected in your text. Confer this link.

0

精彩评论

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

关注公众号