2011-04-28 76 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