Jquery 动态元素绑定时间

来源:互联网 发布:编程原本 编辑:程序博客网 时间:2024/06/05 11:22



1、使用live()进行事件绑定;

比如对于元素的toggle()事件,使用live()绑定事件时,可以通过先为其指定click()事件,然后在click()事件中设置该元素的toggle()事件,最后只要自动触发一次click()事件就可以了。


$("#list .message input").live("click",function(){
    $(this).toggle(
        function(){
            $(this).parent(this).after("<div>评论列表</div>");
        },
        function(){
            $(this).parent(this).next(this).remove();
        }
    ).trigger('click');
});

2、Jquery1.9以前的版本使用live,jQuery 1.9以后的版本动态生成的元素要写成

$(document).on("click", "#ele", function() {

    //...

});

0 0
原创粉丝点击