Direct vs. Delegated - jQuery .on()

前端 未结 5 1488
鱼传尺愫
鱼传尺愫 2020-11-21 05:11

I am trying to understand this particular difference between the direct and delegated event handlers using the jQuery .on() method. Specifically, the last

5条回答
  •  误落风尘
    2020-11-21 05:19

    The explanation of N3dst4 is perfect. Based on this, we can assume that all child elements are inside body, therefore we need use only this:

    $('body').on('click', '.element', function(){
        alert('It works!')
    });
    

    It works with direct or delegate event.

提交回复
热议问题