試試這個:
$(function() {
jQuery(".gallery-item a").click(function(evt) {
jQuery("#imageBox").empty().append(
jQuery("<img />", { src: $(this).attr("href") })
);
return false;
});
});
編輯發表評論:
假設圖像的URL是在屬性 「數據urlimage」 標籤 「一」
Html:
<div class="gallery-item">
<a data-urlimage="/img/myimage.jpg" href="#">click here</a>
</div>
的Javascript:
$(function() {
jQuery(".gallery-item a").click(function(evt) {
var img = new Image();
img.src = $(this).data("urlimage");
img.onload = function(){
jQuery("#imageBox").empty().append(img);
};
return false;
});
});
see image events
編輯:看看如何創造更好的對象 「圖像」
你就不能添加到HTML? – MarcoK
只需在頁面加載時點擊第一個「」即可。 – adeneo