0

我对悬停事件有一个奇怪的问题。当我将鼠标悬停在元素 (.helpImg) 上时,它将在悬停函数内运行我的代码的 4-6 次。我不确定如何调试它。有人有想法吗??非常感谢。

function test(){


console.log('testcall'); //this will only output 1 time

$('.helpImg').hover(function(){

         console.log('call'); //when hover the class, this will output multiple times (4~6 times randomly).          

         //the animation below will play multiple times too
          tooltip.css('display', 'block');
          tooltip.animate({ opacity: 0 }, 0);


          tooltip.animate({'opacity':'1','top':'-=10'},500);

        },function (){            
          $instance = $(this).css({'cursor': 'auto'});
          $('#tooltip-' + $instance.attr('id') ).hide(50);
          $(".js-tutorialTooltips").hide(50);
        })

}
4

1 回答 1

0

您很可能多次注册悬停事件,请尝试此快速修复:

$('.helpImg').not('.registered').addClass('registered').hover(function() { 
  //... your code

这会过滤掉具有 class 的 helpImg 节点,将 classregistered添加registered到其他节点并在其他节点上注册hover函数。

然而,更好的解决方案是找出为什么您的悬停事件会被多次注册。在您的代码中,您似乎有一个 function test,其中包括注册悬停事件。如果您test多次调用,悬停将被多次注册。

所以解决这个问题的最好方法是console.log('register hover')在你的悬停事件处理程序之前,让它只被调用 1 次。

于 2012-08-21T20:21:45.217 回答