$(".spanCont:first .collection_shop").on("click",function(){
var current_item = $(this);
$.ajax({
url: "ajax/abc.php",
type: "POST",
dataType: 'html',
data: {collection_id: current_item.attr("value")},
beforeSend: function(xhr) {
current_item.replaceWith("<div id='temp_div'></div>");
}
}).done(function(data){
$(".spanCont:first .span-2, .spanCont:first input").remove();
$("#temp_div").replaceWith(data);
});
});
此代码应该适用于类.collection_shop的所有静态和动态点击元素,但它只适用于静态元素。jquery事件不适用于动态元素
*如果这些'.collection_shop'添加元素动态 - 值得给予':first'元素它自己的独特类 - 以减少选择器语法。 –