$(".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'元素它自己的獨特類 - 以減少選擇器語法。 –