[英]How to add onclick event on image dynamically
我正在使用jQuery動態創建具有onclick
屬性的圖像
function create() {
divElem = $("<div class='row'>");
$('#bankList').append(divElem);
elem = $("<div class='col-sm-3', style='height:110px'>");
image = $("<img style=' max-width:90%'>");
var imageFile = '${pageContext.request.contextPath}/images/' + prop[j];
image.attr('src', imageFile);
image.attr('id',prop[j]);
image.on("click",submitForm(this.id));
elem.append(image);
divElem.prepend(elem);
}
在winodw.onload
上調用函數create
,而submit form
僅稱為onload
。 點擊圖片時應調用
您的事件處理程序有誤。 您可以改為提供匿名處理程序函數:
image.on("click",function() {
submitForm(this.id);
});
請參閱.on
方法文檔中的詳細信息。
您可以在文檔上添加img onClick函數
$(document).on('click', 'img', function() {
alert('Click on Image id: '+$(this).attr('id'));
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.