admin管理员组文章数量:1426782
How do I highlight (css: background-color) a word with JavaScript when the mouse pointer is hovering over it? It should be possible to select it by clicking on it then and saving it in a variable.
How do I highlight (css: background-color) a word with JavaScript when the mouse pointer is hovering over it? It should be possible to select it by clicking on it then and saving it in a variable.
Share Improve this question asked Mar 27, 2011 at 10:39 DanDan 311 silver badge2 bronze badges 1- possible duplicate of Getting the text under the mouse pointer – Yi Jiang Commented Mar 27, 2011 at 11:15
1 Answer
Reset to default 5var words=$("#yourTextContainer").text().split(' ');
$("#yourTextContainer").html("");
$.each(words, function(i,val){
//wrap each word in a span tag
$('<span/>').text(val+" ").appendTo("#yourTextContainer");
});
$("#yourTextContainer span").live("mouseover",function(){
//highlight a word when hovered
$(this).css("background-color","yellow");
});
$("#yourTextContainer span").live("mouseout",function(){
//change bg to white if not selected
if($(this).css("background-color") !="rgb(0, 0, 255)")
{
$(this).css("background-color","white");
}
});
$("#yourTextContainer span").live("click",function(){
$("#yourTextContainer span").css("background-color","white");
$(this).css("background-color","blue");
//gets the text of clicked span tag
var text = $(this).text();
});
EDIT:See the example http://jsfiddle/aD5Mu/
本文标签: jqueryJavaScript Highlightselect word under mouse pointerStack Overflow
版权声明:本文标题:jquery - JavaScript: Highlightselect word under mouse pointer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745485276a2660353.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论