jQuery 1.7 on() and off() methods for dynamic elements

前端 未结 3 985
误落风尘
误落风尘 2020-12-08 03:13

jQuery 1.7\'s .on() and .off() methods are supposed to replace .live() and such.

I tried it with a dynamic item:



        
3条回答
  •  悲&欢浪女
    2020-12-08 03:47

    See http://blog.jquery.com/2011/11/03/jquery-1-7-released/ for live() -> on/off() (and other) examples.

    This is their example for converting live to on:

    $('a').live('click', fn);
    $(document).on('click', 'a', fn);
    

    So your example becomes to:

    $(document).on('click', '.myList', function(e){
      alert('hello world');
    });
    

提交回复
热议问题