Proper use of .on method in Jquery

前端 未结 6 1770
慢半拍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:36

    To replace .live() you need one more parameter int he .on() call.

    // do not use! - .live(events, handler)
    $('#container a').live('click', function(event) {
        event.preventDefault();
        console.log('item anchor clicked');
    });
    
    // new way (jQuery 1.7+) - .on(events, selector, handler)
    $('#container').on('click', 'a', function(event) {
        event.preventDefault();
        console.log('item anchor clicked');
    });
    

    Source: http://www.andismith.com/blog/2011/11/on-and-off/

提交回复
热议问题