Javascript 如何在包含iframe的div上处理onclick事件?
单击图像时如何触发Javascript 如何在包含iframe的div上处理onclick事件?,javascript,html,iframe,onclick,Javascript,Html,Iframe,Onclick,单击图像时如何触发onclick事件? 一个限制是我不能修改testcode2.html 现在href属性只起作用。 请帮帮我:( 在testcode2.html上移动脚本 <a href="javascript:go()"> <img src="http://cdn01.cdn.pinkisthenewblog.com/wp-content/uploads/2012/07/071712_findingnemosequelfeat-250x250.jpg"/></a
onclick
事件?一个限制是我不能修改testcode2.html 现在
href
属性只起作用。请帮帮我:(
在testcode2.html上移动脚本
<a href="javascript:go()">
<img src="http://cdn01.cdn.pinkisthenewblog.com/wp-content/uploads/2012/07/071712_findingnemosequelfeat-250x250.jpg"/></a>
然后在发出警报后通过javascript进行重定向,以调用父级使用
窗口中定义的javascript函数。父级
<a href="http://translate.google.com" onclick="window.parent.go();">
<img src="http://cdn01.cdn.pinkisthenewblog.com/wp-content/uploads/2012/07/071712_findingnemosequelfeat-250x250.jpg"/>
</a>
在这种情况下,您可能需要停止重定向,在go()函数中添加一个return false
,然后使用onclick=“return window.parent.go();”
给div一个ID,然后使用jQuery触发onclick事件
<script type="text/javascript" src="jquery-1.10.2.min.js">
$( document ).ready(function() {
$("#divIFrame").click(function(){
alert("Hello World");
});
});
</script>
<div href="#" ID="divIFrame">
<iframe src="testcode2.html" />
</div>
$(文档).ready(函数(){
$(“#divIFrame”)。单击(函数(){
警报(“你好世界”);
});
});
window.onload=函数(){
document.getElementById(“测试框架”).contentWindow.document.body.onclick=function(){
警报(“你好,世界!”);
};
};
编辑:如果不能使用getElementById(),则querySelector()或getElementsByTagName()可以工作
<script>
window.onload = function() {
document.querySelector("iframe").contentWindow.document.body.onclick = function(){
alert("Hello World!");
};
/* document.getElementsByTagName("iframe")[0].contentWindow.document.body.onclick
= function(){
alert("Hello World!");
}; */
};
</script>
<iframe id="test_frame" src="testcode2.html" />
window.onload=函数(){
document.querySelector(“iframe”).contentWindow.document.body.onclick=function(){
警报(“你好,世界!”);
};
/*document.getElementsByTagName(“iframe”)[0].contentWindow.document.body.onclick
=函数(){
警报(“你好,世界!”);
}; */
};
这可能会对您有所帮助
jQuery
$(document).ready(function(){
$(".frame").on('click', call);
});
function call(){
alert("I am called.");
}
HTML
testcode2.html是否与主html位于同一域中?否。它们彼此是不同的域。您可以将相同的脚本传输到iframe页面并运行。嘿,我看了看,它在您的情况下不起作用。我发现它需要iframe中的空白空间才能工作。如果您让我知道您到底想做什么,我可以帮助您。?哦,真的很感谢Mayank Tripathi:)实际上我想点击一个adsense广告。adsense广告('a'标记)将在iframe中制作,因此当在iframe中点击广告时,我想调用在父页面中编写的方法。尝试在绑定中使用单引号而不是双引号:$('#divIFrame')。单击(函数(){谢谢!但实际上iframe是动态创建的,所以我不能给iframe“id”。如果您不能使用id,我想您可能需要使用querySelector()或getElementsByTagName()来查找iframe。
<script>
window.onload = function() {
document.getElementById("test_frame").contentWindow.document.body.onclick = function(){
alert("Hello World!");
};
};
</script>
<iframe id="test_frame" src="testcode2.html" />
<script>
window.onload = function() {
document.querySelector("iframe").contentWindow.document.body.onclick = function(){
alert("Hello World!");
};
/* document.getElementsByTagName("iframe")[0].contentWindow.document.body.onclick
= function(){
alert("Hello World!");
}; */
};
</script>
<iframe id="test_frame" src="testcode2.html" />
$(document).ready(function(){
$(".frame").on('click', call);
});
function call(){
alert("I am called.");
}
<div class="frame">
<iframe src="http://www.jsfiddle.net/" name="iframe_a"></iframe>
</div>