2013-04-11 35 views
0

我不知道如何實現從另一個事件,當他們同時觸發事件的解除綁定。我有這樣的HTML層次結構:jquery同時事件關係

<div class="first"></div> 
<div class="second"></div> 

當鼠標輸入.first,.second顯示。然後,如果鼠標離開。首先,。秒應該隱藏,除非鼠標正在進入。秒。因此,我的問題是同時觸發$('。first')。mouseleave()& $('。second')。mouseenter()。我試過類似的東西,沒有結果:

$('.first').mouseenter(function(){ 
    $('.second').show(); 
}); 

$('.first').mouseleave(function(){ 
    $('.second').hide(); 
}); 

$('.second').mouseenter(function(){ 
    $('.first').unbind('mouseleave'); 
}); 

$('.second').mouseleave(function(){ 
    $('.first').bind('mouseleave'); 
    $('.second').hide(); 
}); 

任何線索?

以下是示例:http://jsfiddle.net/XdU7H/

+0

你沒有一個對的mouseenter。首先..? – 2013-04-11 16:35:57

+0

創建[jsfiddle示例](http://jsfiddle.net)。 – Dom 2013-04-11 16:41:13

+0

對,發佈編輯! – Joeyjoejoe 2013-04-11 16:41:31

回答

1

超時!

var hideTimer; 

$('.first').mouseenter(function(){ 
    $('.second').show(); 
}); 

$('.first').mouseleave(function(){ 
    hideTimer = setTimeout(function() { 
     $('.second').hide(); 
    }, 500); 
}); 

$('.second').mouseenter(function(){ 
    clearTimeout(hideTimer); 
    $('.first').unbind('mouseleave'); 
}); 

$('.second').mouseleave(function(){ 
    $('.first').bind('mouseleave'); 
    $('.second').hide(); 
}); 

根據您的需求調整它,因爲我不確定我是否正確地遵循您的邏輯。 :P

http://javascript.info/tutorial/events-and-timing-depth

+0

這是幾乎沒有變化。非常感謝,這篇文章幫助我理解了錯誤。 – Joeyjoejoe 2013-04-11 18:53:30