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

给你一囗甜甜゛ 提交于 2019-11-28 04:44:15

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');
});

Here is a little example:

http://jsfiddle.net/zzSjK/

<script type="text/javascript">
    $(function(){
        $(document).on('click','.clickme' , function(e){
          addtext()
        });
        function addtext() {
            $('.myList').append('<div class="clickme">click me</div>')
        }
    })
</script>

<div class="myList">
    <div class="clickme">-click-</div>
</div>

"Bind" with:

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

And "unbind" with:

$(document).off('click','.myDiv');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!