2011-04-28 82 views
0

我有以下代碼:jQuery的鼠標事件處理

HTML:

<div class="one">Content</div> 
<div class="two">Content</div> 

我想隱藏我的第二個divmosueleave事件從第一div發生,也鼠標沒有過第二個div

算法:

if ((mouseleave from div.one) && (mouse not on div.two)) 
    hide (div.two); 

如何使用jQuery我實現了這個片段?請幫幫我。

回答

2

您可以在.two的div跟蹤的鼠標懸停狀態的設置一個標誌。然後,當鼠標離開.one時,檢查此狀態,如果存在,則隱藏div。像這樣:

$(".two").live("mouseenter", function(){ 
    $(this).data("hover", true); 
}).live("mouseleave", function(){ 
    $(this).removeData("hover"); 
}); 

$(".one").live("mouseleave", function(){ 
    if(!$(".two").data("hover")) $(".two").hide(); 
}); 
1

同時包含div s在另一箇中,比如div class='zero'。所以你會在你的$(document).ready()

$('.zero').live('hover', function() { 
    $('.two').show(); 
}); 

$('.zero').live('blur', function() { 
    $('.two').hide(); 
}); 

注意:必須爲style="display: none"通過div class='two'默認

+0

嗨,朋友,這是不可能改變的HTML。所以給一些替代解決方案。 – thecodeparadox 2011-04-28 08:40:51