jQuery on()给动态元素绑定事件

来源:互联网 发布:iphone6plus在线软件 编辑:程序博客网 时间:2024/05/14 15:27

举例:

    // 点击收藏    $('.content').on('click', '.iconfont-heart', function() {        var num_wp = $(this).siblings('.collect-num');        var num_value = parseInt(num_wp.text());        if($(this).hasClass('iconfont-heart-full')){            num_wp.text(num_value-1);        }else{            num_wp.text(num_value+1);        };        $(this).toggleClass('iconfont-heart-empty iconfont-heart-full');        $(this).siblings('.collect-num').toggleClass('on');    });

解释:
.content : 非动态添加的父级元素;
iconfont-heart : content下面的子元素,也是需要click事件的绑定对象 ;
this : 子元素对象 ;
hasClass() : 判断元素是否包含指定的类,包含则返回true;

1 0
原创粉丝点击