使用jquery1.10版本,动态在一个div.里面增加a元素,包含有span子元素。我要在span子元素上添加一个click事件,点击span元素删除整个a元素,但是试了很多方法都不行,直接用clik ,不行,用on()方法也不行。 $(".tags .btn_confirm").click(function(){ var text = $(this).text(); //添加的a元素
var node = $("<a href='#' class='tseleced btn_confirm'>"+text+ "<span class='tags_closebtn'>X</span></a>");
if($(".tags_selected").children().length >= 8){
$(".selector_warn").show();
}else{
$(".selector_warn").hide();
$(".tags_selected").append(node);
}
}) //这里使用on(),方法,console.log()没有打印任何东西
$(".tags_closebtn").on("click",function(e){ console.log(e);
$(this).prev().remove()
})
1 回答

梦里花落0921
TA贡献1772条经验 获得超6个赞
使用live
$("button").live("click",function(){ });
原因是jquery的事件绑定在页面加载时就已经完成,所以之后动态添加的class将无法绑上事件,所以使用live可以解决这个问题。不过live只支持jquery1.9以前(大概),之后版本的就可以使用
$('body').on("click", ".tags_closebtn", function () { });
添加回答
举报
0/150
提交
取消