[英]JQuery click event for element from parent page
I am using jquery-1.8.3, and trying to ovveride onclick event for an image html element. 我正在使用jquery-1.8.3,并尝试为图像html元素设置ovveride onclick事件。
<input id=ClientId1_imgImageID title="Click for larger view" style="BORDER-LEFT-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-TOP-WIDTH: 0px" type=image name=ClientId1$imgImageID ImageID="3baf5b7b-2246-4be2-a21f-f5b7956e0010"></input>
The problem is that this element is generated dynamicly. 问题在于该元素是动态生成的。 I could successfuly remove onclick attribute using JQuery code :
我可以使用JQuery代码成功删除onclick属性:
function UpdateOnclick(ClientID){
window.opener.$("#" + ClientID + "_imgImageID").removeAttr("onclick");
}
But when I try to add click event, I am failed. 但是,当我尝试添加点击事件时,我失败了。 I`ve tried different methods, but none of them are working :
我尝试了不同的方法,但是没有一个起作用:
Method 1: 方法1:
window.opener.$("#" + ClientID + "_imgImageID").click(function() { alert('test'); return false; });
Method 2: 方法2:
$(window.opener.document).on("click", "#" + ClientID + "_imgImageID" ,function() { alert('test'); return false; });
Can anybody help me to understand why onclick event is not applied to , please ? 请问有人可以帮助我理解为什么onclick事件不适用吗?
For dynamically added elements you should use Method 2, but u have a mistake there. 对于动态添加的元素,您应该使用方法2,但是在那里您有一个错误。
Function should look like this: 函数应如下所示:
$(window.opener.document).on("click", "#" + ClientID + "_imgImageID" ,function() { alert('test'); return false; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.