举例:
// 点击收藏
$('.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;