使用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()
})
jq動態添加的元素使用on()無法綁定click事件
qq_笑_17
2018-09-07 11:10:44