If I for example have
<p> some long text </p>
on my HTML page, how can I know that cursor of mouse is for example above the word 'text'?
If I for example have
<p> some long text </p>
on my HTML page, how can I know that cursor of mouse is for example above the word 'text'?
My answer is derived from Drakes' "Solution 2 - Caret inspection and DOM traversal". Thanks a lot to Drakes for pointing to this solution!
However, there's two problems with Drakes' solution 2 when working on IE. (1) the offset as calculated is incorrect, and (2) too complex, lots of code.
See my demonstration on JSFiddle at here.
For problem 1, if you click somewhere at about the last line of the text, for example somewhere in "shoulder pork loin shankle turducken shank cow. Bacon ball tip sirloin ham.", you can notice the offset calculation is different with IE (original solution) and IE method 2 (my solution). Also, the results from IE method 2 (my solution) and from Chrome, Firefox are the same.
My solution is also much simpler. The trick is, after use TextRange to make selection at the absolute X/Y position, get a type of IHTMLSelection by calling document.getSelection(). This does not work for IE<9 but if that's OK for you, this method is much simpler. Another caveat is, with IE the method's side effect (same as the original method) is change of selection (i.e. losing user's original selection).
My other answer works only in Firefox. This answer works in Chrome. (Might work in Firefox, too, I don't know.)
In your mousemove handler, call
getWordAtPoint(e.target, e.x, e.y);
Aw yiss! Here is ho!
Simple as it is and whitout Jquery or anyother framework Fiddle: https://jsfiddle.net/703c96dr/
It will put spans on each word and add a onmouseover and onomouseout function. I could create a simple class to make it more usable but the code is so simple that anyone can edit and use.
Simple code
Here's a simple solution that works in Chrome for most cases:
I leave filtering out punctuation and properly handling hyphenated words as an exercise to the reader :).
You would probably have to break up the paragraph so that each word was contained inside of its own separate <span> element and then add
onmouseover
event attributes to each of them...And I think you mean "<p>some long text</p>"; backslashes are not part of HTML.