triggerHandler vs. trigger in jQuery

后端 未结 4 655
暖寄归人
暖寄归人 2020-12-01 06:33

Out of curiosity -- what is the purpose of / use cases for jQuery\'s triggerHandler? As far as I can tell, the only \"real\" differences between trigger

4条回答
  •  日久生厌
    2020-12-01 07:05

    What is the advantage to ensuring the native event does not fire?

    • You have actions bound to a 'focus' event but you don't want the browser to focus really focus it (might seem dumb but it could happen, couldn't it? like a code that you would like to execute once without losing the current focus).

    • A component that you are making want to trigger 'load' (just an example of a generic thing) of another component that is inside it.

      In that case, if you are calling 'load' of children when 'load' of the parent comes, you don't want to do this because it would cause an infinite call if the event.stopPropagation isn't called by the listeners of 'load' event (caused by bubling):

    $container.on('load', function () {
        $somethingInsideContainer.trigger('load'); 
        // Would cause a loop if no event.stopPropagation() is called
    });
    

    In that case you have to call triggerHandler().

提交回复
热议问题