jQuery事件触发两次 [英] jQuery event triggers twice

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

问题描述

有人向我解释为什么这个事件会触发两次?在1.7之前的jQuery版本上似乎没有这样做。

 < input type =textboxid =框onblur =console.log('这将触发两次!');/> 
< script>
$('#box')。blur();
< / script>

小提琴:
http://jsfiddle.net/EWbmD/52/

解决方案

从内部看起来,触发 blur 事件触发 blur focusout 事件内部调用 onblur 两次。



PoC

  $('#box')。on('blur focusout',function(e){
console.log('this too',e .type);
});
$('#box')。blur();

演示:小提琴



blur 事件是一个非冒泡的事件,冒泡对方是 foucsout 事件。所以在正常情况下,模糊操作会触发这两个事件。所以jQuery正在努力做到聪明,并且在发生模糊事件时触发这两个事件,但是看起来实际上有一个错误。


Can someone explain to me why this event triggers twice? It doesn't seem to do it on jQuery versions prior to 1.7.

<input type="textbox" id="box" onblur="console.log('This will trigger twice!');"/>
<script>
$('#box').blur();
</script>

Fiddle: http://jsfiddle.net/EWbmD/52/

解决方案

From the internals, it looks like triggering blur event is triggering blur and focusout events internally which is invoking the onblur twice.

PoC

$('#box').on('blur focusout', function (e) {
    console.log('this too', e.type);
});
$('#box').blur();

Demo: Fiddle

The blur event is a non bubbling event, the bubbling counterpart is the foucsout event. So in normal circumstances the blur operation triggers both these events. So jQuery is trying to be intelligent and fires both these events in case of a blur event, but it looks like there is an bug in the implementation.

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

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