in this case:
<p> some long text </p>
how can i know that mouse cursor is above the 'text' word?
in this case:
<p> some long text </p>
how can i know that mouse cursor is above the 'text' word?
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.
To my knowledge, you can't.
Only thing I can think of is to put each of the words in their own element, then apply mouse over events to those elements.
<p><span>Some</span> <span>long</span> <span>text</span></p>
<script>
$(document).ready(function () {
$('p span').bind('mouseenter', function () {
alert($(this).html() + " is what you're currently hovering over!");
});
});
</script>
Further to the two other answers, you may be able to split your paragraphs up into spans using jQuery (or javascript generally).
That way, you wouldn't need to think about outputting your text with spans around the words. Let your javascript do it for you.
e.g.
<p>Each word will be wrapped in a span.</p>
<p>A second paragraph here.</p>
Word: <span id="word"></span>
<script type="text/javascript">
$(function() {
// wrap words in spans
$('p').each(function() {
var $this = $(this);
$this.html($this.text().replace(/\b(\w+)\b/g, "<span>$1</span>"));
});
// bind to each span
$('p span').hover(
function() { $('#word').text($(this).css('background-color','#ffff66').text()); },
function() { $('#word').text(''); $(this).css('background-color',''); }
);
});
</script>
Note that the above code, while it works, will strip out any html inside your paragraph tags.
In Firefox you can hook the mousemove event. The callback has one argument, e. In the callback, do this:
var range = HTTparent.ownerDocument.createRange();
range.selectNode(e.rangeParent);
var str = range.toString();
range.detach();
Now str has the entire text that the mouse was over. e.rangeOffset is the location of the mousepointer within that string. In your case, str would be "some long text" and e.rangeOffset would be 11 if you were over the "e" in "text".
This code will get a little confused if you are in the margins, for instance when the mouse pointer is on the same line as the text but after the end of it. To fix this, you need to check that you are actually on top of text. Here's the test:
if(e && e.rangeParent && e.rangeParent.nodeType == e.rangeParent.TEXT_NODE
&& e.rangeParent.parentNode == e.target)
This technique works in Firefox. Doesn't work in Chrome.
My other answer works only in Firefox. This answer works in Chrome. (Might work in Firefox, too, I don't know.)
function getWordAtPoint(elem, x, y) {
if(elem.nodeType == elem.TEXT_NODE) {
var range = elem.ownerDocument.createRange();
range.selectNodeContents(elem);
var currentPos = 0;
var endPos = range.endOffset;
while(currentPos+1 < endPos) {
range.setStart(elem, currentPos);
range.setEnd(elem, currentPos+1);
if(range.getBoundingClientRect().left <= x && range.getBoundingClientRect().right >= x &&
range.getBoundingClientRect().top <= y && range.getBoundingClientRect().bottom >= y) {
range.expand("word");
var ret = range.toString();
range.detach();
return(ret);
}
currentPos += 1;
}
} else {
for(var i = 0; i < elem.childNodes.length; i++) {
var range = elem.childNodes[i].ownerDocument.createRange();
range.selectNodeContents(elem.childNodes[i]);
if(range.getBoundingClientRect().left <= x && range.getBoundingClientRect().right >= x &&
range.getBoundingClientRect().top <= y && range.getBoundingClientRect().bottom >= y) {
range.detach();
return(getWordAtPoint(elem.childNodes[i], x, y));
} else {
range.detach();
}
}
}
return(null);
}
In your mousemove handler, call getWordAtPoint(e.target, e.x, e.y);
This a live demo for how to get a word under cursor using JavaScript based on the source code provided by Damovisa: http://jsfiddle.net/5gyRx/.