2014-11-17 62 views
0

我试图做一些悬停和关闭.mail img悬停。悬停事件正在发生,其中的代码正在执行,但悬停事件不是。关闭悬停功能不能解雇

为了简化它,我用警报替换了我的代码,并且关闭悬停警报也未触发。

$('.mail img').hover(function(){ 
    alert('on hover'); 
}, 
function(){ 
    alert('off hover'); 
}); 

我使用jQuery 1.9.1

回答

5

此行为是因为你使用alert来测试。用于悬停的事件是mouseleave。当从mouseenter显示alert时,这不会触发。

更改代码,使用console.log(你应该调试时),它会工作:

$('.mail img').hover(function(){ 
    console.log('on hover'); 
}, function(){ 
    console.log('off hover'); 
}); 

Example fiddle

+0

我知道使用警报是不好的做法。我只用了大约10%的时间。但是关闭悬停事件仍然不会与console.log()一起触发。我的代码实际上并没有改变。 – wordSmith

+0

您是否检查了控制台的错误?你可以从小提琴中看到,你提供的代码应该工作正常。 –

+0

不知道为什么关闭悬停,mouseleave,也没有mouseout射击。没有错误。代码全部有效。感谢教育,不知道警惕会悬停悬停事件。 – wordSmith

0

您可以使用mouseout()检查光标离开elemnt

$('.mail img').mouseout(function(){ 
    alert('off hover'); 
}); 
0

使用此项:

$('.mail img').mouseover(function(){ 
    alert('on hover'); 

}).mouseout(function(){ 
    alert('off hover'); 

});