jQuery事件触发两次 [英] jQuery event triggers twice
问题描述
< 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屋!