Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  181] [ 7]  / answers: 1 / hits: 34937  / 15 Years ago, mon, march 15, 2010, 12:00:00

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'?


More From » browser

 Answers
7

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.



jsFiddle example


[#97339] Thursday, March 11, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mira

Total Points: 460
Total Questions: 108
Total Answers: 99

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
mira questions
;