1. 程式人生 > >jQuery繫結img的click事件

jQuery繫結img的click事件

用JQUERY給IMGelement繫結click事件的時候,直接用img.click(function(){...})不起作用,如下面程式碼
$("img.ms-rteImage-LightBox").click(function(){
SP.UI.ModalDialog.showModalDialog({
url: $(this).attr("src"),
title: $(this).attr("alt")
});
});

解決這個問題需要用jquery裡面bind函式去附加click event. 如下:

$("img.ms-rteImage-LightBox").bind("click",function(){
SP.UI.ModalDialog.showModalDialog({
url: $(this).attr("src"),
title: $(this).attr("alt")
});
});