What's the difference between `on` and `live` or `bind`?

前端 未结 7 1957
迷失自我
迷失自我 2020-11-22 16:00

In jQuery v1.7 a new method, on was added. From the documentation:

‘The .on() method attaches event handlers to the currently se

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 16:27

    Something you should be aware of if you want to get the event handlers associated with the element - pay attention which element the handler was attached to!

    For example, if you use:

    $('.mySelector').bind('click', fn);
    

    you will get the event handlers using:

    $('.mySelector').data('events');
    

    But if you use:

    $('body').on('click', '.mySelector', fn);
    

    you will get the event handlers using:

    $('body').data('events');
    

    (in the last case the relevant event object will have selector=".mySelector")

提交回复
热议问题