我想知道,如何創建選擇器,鏈接到動態創建的元素,而不使用動態創建的元素上的事件?如何創建選擇器,鏈接到動態創建的元素?
注:動態創建的元素我的意思是元素,是不是在HTML開頭
我知道,我可以用$(本),當我有事件(例如。點擊)的元素,但是當我沒有事件,我需要動態創建的元素選擇器?
感謝所有。
更新:這裏是我的情況:
$(".product div a").click(function() {
var nameOfProduct = $(this).parent().find("h3").text();
var dataPrice = parseInt($(this).parent().find("div b").text());
var isProductIn = false;
var itemsInDay = $(/*HERE I NEED THE SELECTOR*/).find("div.item").nextUntil("div.center");
itemsInDay.each(function() {
if (nameOfProduct == ($(this).find("span ins span").text())) {isProductIn = true;};
});
if (isProductIn) {
//add only +1, not new product again;
} else {
var imgOfProduct = $(this).parent().parent().find("img").attr('src').replace('med', 'min');
var product = ('<div class="item" data-price="' + dataPrice + '"><img src="' + imgOfProduct + '"><span><ins><b>1x</b> ' + nameOfProduct + '</ins><a href="#"><img src="images/delete.png"></a></span></div>');
$(product).appendTo(".dayOrder:last").delay(800).hide().slideDown();
}
return false;
});
如何創建動態創建的元素?一些代碼? – dfsq
在創建元素時爲元素分配變量,然後可以繼續使用它來操作元素。 –
你爲什麼需要這種行爲?你最好告訴你在找什麼,爲什麼你需要'動態'選擇器?! –