JavaScript / jQuery:事件触发两次 [英] JavaScript/jQuery: event fired twice
问题描述
$(文件).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屋!