JavaScript / jQuery:事件触发两次 [英] JavaScript/jQuery: event fired twice

查看:102
本文介绍了JavaScript / jQuery:事件触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery尝试一些事件绑定,我的文档中有这个简单的代码准备好了:

  $(文件).ready(function(){
$(pa)。each(function(i,selected){
$(selected).hover(
function(event){alert ('over:'+ event.type);},
function(event){alert('out:'+ event.type);});
}

});

所以,当我将鼠标移到一个段落内的链接(选择器是pa)时,弹出两次。所以,鼠标和鼠标点击两次。



这只会发生在Google Chrome中。在Internet Explorer 7中, Firefox 3, Opera Safari 这两个事件只会触发一次。



为什么会这样?

解决方案

我以某种方式提醒,实际上导致Chrome中的额外事件。使用以下代码,我只看到一个事件。

  $(document).ready(function(){
$ (pa)。each(function(i,selected){
$(selected).hover(
function(event){$(p)。append('< br> over :'+ event.type);},
function(event){$(p)。append('out:'+ event.type);});
}

});


I'm trying some event bindings with jQuery, and I have this simple code in my document ready:

$(document).ready(function() {
    $("p a").each(function(i, selected){
        $(selected).hover(
            function(event) { alert('over: ' + event.type); },
            function(event) { alert('out: ' + event.type); });
        }
    )
});

So, when I move mouse over a link thats inside a paragraph (selector is "p a") alerts pop-up twice. So, mouseenter and mouseleave are fired twice.

This only happens in Google Chrome. In Internet Explorer 7, Firefox 3, Opera and Safari both events are fired only once.

Why is this happening?

解决方案

I think somehow the alert is actually causing the extra events in Chrome. With the following code I only see one event.

$(document).ready(function() {
  $("p a").each(function(i, selected){
    $(selected).hover( 
      function(event) { $("p").append('<br>over: ' + event.type); }, 
      function(event) { $("p").append(' out: ' + event.type); });
    }
  )
});

这篇关于JavaScript / jQuery:事件触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆