首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >奇怪的悬停事件问题

奇怪的悬停事件问题
EN

Stack Overflow用户
提问于 2012-08-22 04:17:01
回答 1查看 59关注 0票数 0

关于悬停事件我有一个奇怪的问题。当我悬停到元素(.helpImg)时,它会在悬停函数中运行我的代码的4-6次。我不确定如何调试它。谁有主意??非常感谢。

代码语言:javascript
复制
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);
        })

}
EN

回答 1

Stack Overflow用户

发布于 2012-08-22 04:21:45

您很可能多次注册hover事件,尝试以下快速修复方法:

代码语言:javascript
复制
$('.helpImg').not('.registered').addClass('registered').hover(function() { 
  //... your code

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

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

因此,处理这个问题的最好方法是在悬停事件处理程序之前执行console.log('register hover'),并使其只被调用一次。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12062386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档