I searched everywhere but can't find it. So I'm asking it here. I want to get the text when the mouse is on top (hover) of that text inside an html page. I tried using getClientRect, but they only give me coordinates, and not text. Does anybody know how to do this with javascript or html? Thanks.
I searched everywhere but can't find it. So I'm asking it here. I want to get the text when the mouse is on top (hover) of that text inside an html page. I tried using getClientRect, but they only give me coordinates, and not text. Does anybody know how to do this with javascript or html? Thanks.
Share Improve this question asked Mar 25, 2012 at 22:52 user1291899user1291899 411 silver badge4 bronze badges 3- Can't you give us an HTML markup???? What kind of text is it? how many "texts" do you have in the page? – gdoron Commented Mar 25, 2012 at 22:56
- ...*searched everywhere but can't find it* hmmm? – Caffeinated Commented Mar 25, 2012 at 22:57
- here's an example <html> <body> hello, i am new </body> </html> So when I hover my mouse over "am", I would be able to retrieve "am" – user1291899 Commented Mar 25, 2012 at 23:00
2 Answers
Reset to default 2It depends on what element you're trying to get the text for, but possibly what you want to do is use the mouseenter event.
$(parentselector).on({
'mouseenter': function(){
alert($(this).html()); // .html() or .val() depending on the element
}
},
targetselector
);
EDIT:
How to get a word under cursor using JavaScript?
Create your HTML like this
<html>
<head></head>
<body>
<span class="word">Hello</span>
<span class="word">I</span>
<span class="word">am</span>
<span class="word">new</span>
</body>
</html>
Then something like this in jQuery.
$('body').on({
'mouseenter': function(){
var txt = $(this).html();
}
},
'span.word'
);
If you've got access to jQuery, you can use the .mouseover() event handler. You can bind it to a specific DOM element and then grab the value either through val, or innerhtml.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745118948a4612299.html
评论列表(0条)