jQuery event triggers twice

后端 未结 3 634
不知归路
不知归路 2021-02-11 04:11

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



        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-11 04:25

    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.

提交回复
热议问题