當鼠標指針懸停在JavaScript上時,如何使用JavaScript突出顯示(css:background-color)單詞?應該可以通過點擊選擇它並將其保存在一個變量中。JavaScript:突出顯示/選擇鼠標指針下的單詞
2
A
回答
4
var 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();
});
編輯:參見示例http://jsfiddle.net/aD5Mu/
+0
這是一個很好的答案,但請記住,這可能會很慢,大量的文本。 – tobint 2011-03-27 11:11:21
+0
嗯..可能會慢..不知道是否有更好的更快的方法.. – 2011-03-27 11:15:39
+0
懸停樣式可以用CSS來完成,'委託'會減少事件在擊中之前必須經過的元素數量處理程序。否則,可能沒有其他方法可以高效地完成這個 – 2011-03-27 11:17:08
相關問題
- 1. emacs xterm鼠標模式雙擊以突出顯示單詞
- 2. 如何從鼠標的段落中選擇和突出顯示單詞,即使在jquery或javascript中?
- 3. 如何突出顯示替換所選單詞的單詞?
- 4. Safari瀏覽器:獲取鼠標指針下的單詞
- 5. 突出顯示ListView選擇
- 6. 如何創建單詞選擇器以選擇並突出顯示並從文本中移除所選單詞?
- 7. 如何使用鼠標指針在tcl/tk上突出顯示一個對象?
- 8. 突出顯示DataGridView中的單詞?
- 9. JavaScript:從頁面中刪除當前的鼠標突出顯示?
- 10. 嘗試選擇具有較低Z指標的鼠標(突出顯示文本)div的內容
- 11. 我需要突出顯示單詞
- 12. Visual Studio單詞突出顯示插件
- 13. mark.js不突出顯示單詞?
- 14. 轉到哪裏單詞突出顯示
- 15. 突出顯示一堆單詞?
- 16. 在RichEditBox中突出顯示單詞
- 17. Vim突出顯示單詞列表
- 18. 在longpress上突出顯示單詞android
- 19. 從數組中突出顯示單詞
- 20. 如何在鼠標上突出顯示IKImageBrowserView的單元格?
- 21. 根據javascript中的字數突出顯示單詞
- 22. 在按下時突出顯示textview中的單詞
- 23. 突出顯示單詞末尾的單詞
- 24. Visual Studio插件;突出顯示所有匹配選定單詞的單詞?
- 25. 突出顯示Python中的選擇框
- 26. 在一個元素中選擇文本(類似於用鼠標突出顯示)
- 27. 突出顯示WebPage中顯示的文檔中的單詞
- 28. 獲取鼠標指針下的文本
- 29. 獲取鼠標指針下的文本
- 30. 突出顯示從鍵盤輸入選擇標籤選項
[獲取鼠標指針下的文本]的可能重複(http://stackoverflow.com/questions/2183335/getting-the-text-under-the -mouse-pointer) – 2011-03-27 11:15:29