2011-05-03 15 views
1

我有一個div與class ='divclass'和其中的一些文本...現在我想顯示一個彈出式菜單,當用戶鼠標懸停在div中的任何單詞...彈出窗口也會顯示該單詞?哪個用戶具有mousedover鼠標......我們如何才能做到這一點..jquery得到選定的詞

+0

如果你可以拆分html源代碼中的單詞(例如用span來包裝每個單詞),這將很容易。然後你可以綁定mouseover/mouseout事件來獲得你的結果。你是否認爲你可以改變HTML(我的意思是你有權這樣做) – CrisDeBlonde 2011-05-03 08:01:33

回答

0

我同意bicccio。爲了節省時間,你還可以自動化周圍用下面的代碼的每個字跨距創建,然後安裝該事件顯示文本:

$(function() { 
    var text = $(".divclass").text(); 
    text = "<span>" + $.trim(text) + "</span>"; 
    text = text.replace(/\s/g, "</span><span>&nbsp;") 
    $(".divclass").html(text); 

    $(".divclass span").live("mouseover", function() { 
     alert($(this).text()); 
    }); 
}); 
1

我認爲你必須與周圍像<span>一個元素,那麼任何字:

$('.divclass span').mouseover(function(){ 
    alert($(this).text()); 
    // or showMyPopup($(this).text()); 
}); 
0

試試這個:

$(document).ready(function(){ 
var all_words=$("#divclass").text().split(' '); 
$("#divclass").html(""); 

$.each(all_words, function(i,val) 
{ 
$('<span/>').text(val +" ").appendTo("#divclass"); 

}); 
$("#divclass span").live("mouseover",function(){ 
    alert($(this).text()); 
}); 

}); 

看到它住在這裏:http://jsfiddle.net/jqLw8/

0

你可以這樣做:

function divideTextInSpans(text){ 
    return $.map(text.split(" "), function(word){ 
     return '<span class="word">'+word+'</span>'; 
    }).join(' '); 
} 

$(document).ready(function(){ 
    $(".divclass").each(function(){ 
     var newInnerHtml = divideTextInSpans($(this).text()); 
     $(this).html(newInnerHtml); 
     $(this).find(".word").hover(function(){ 
      //Show your popup code here 
      $(this).css("backgroundColor", "yellow"); //Highlight 
     }, function(){ 
     //Hide your popup code here 
      $(this).css("backgroundColor", ""); //Un-Highlight 
     }); 
    }); 
}); 

我們」什麼重做是將每個單詞放在div中的一個單獨的跨度中,然後綁定一個懸停事件。