我使用jquery動態地將圖像添加到我的頁面。基本上,我在頁面上創建圖像的「pixel-y/lo-res」版本,並在原稿的頂部添加頁面加載。然後,一個典型的「淡出懸停」的東西應該在鼠標懸停時淡出它們,顯示出orignals ......就好像「up-rezing」..如果這是有道理的。將事件偵聽器附加到jQuery中的動態生成的圖像?
所以我有圖像進來..但不知何故,懸停監聽沒有附加。起初,我嘗試懸停,現在我點擊只是因爲它更容易排除故障。沒有。
.on()函數應該附加自己,甚至動態添加項目,對吧?出了什麼問題?我沒有收到任何錯誤。這只是不工作。
$.ajax({
type:"POST",
url:"js/pixelsandwich/pixelsandwich.php",
data:{src:src},
success:function(response){ // if there is already a "lo-rez" image, I get that URL.. if there isn't, one is created and I return that URL
newImg = $("<img class='crunched'>"); // create the new <img>
newImg.attr('src', response); // assign the source
frame = $that.parent(); // grab the containing frame
frame.append(newImg); // add the new image. css:position:absolute and z-index take care of the rest
}
});
$(".crunched").on("click", function(){ // << this just isn't attaching at all
alert("hello");
$(this).fadeTo('slow',0.5);
});
有人幫忙嗎?