Proper use of .on method in Jquery

前端 未结 6 1766
慢半拍i
慢半拍i 2020-12-11 17:13

I really liked the .live method as it was straightforward and essentially not much different than your standard event handler.

Alas, it was deprecated and I\'m left

6条回答
  •  旧巷少年郎
    2020-12-11 17:52

    Nope.

    $( '#who_me' ).live( 'click', function () { ... });
    

    is the same as:

    $( document ).on( 'click', '#who_me', function () { ... });
    

    However, you usually don't want to bind to much handlers to the document object. Instead you bind to the nearest static ancestor (of #who_me, in this case). So:

    $( '#wrapper' ).on( 'click', '#who_me', function () { ... });
    

    where #wrapper is an ancestor of #who_me.

提交回复
热议问题