admin管理员组文章数量:1357307
How to fire onclick
event when click the image ?
and one limitation is that I can't modify testcode2.html.
now the href
properties only work.
please help me :( !!
<script>
function go()
{
alert("Hello World!");
}
</script>
<div href="#" onClick="go()">
<iframe src="testcode2.html" />
</div>
testcode2.html
is as follows:
<a href="">
<img src=".jpg"/></a>
How to fire onclick
event when click the image ?
and one limitation is that I can't modify testcode2.html.
now the href
properties only work.
please help me :( !!
<script>
function go()
{
alert("Hello World!");
}
</script>
<div href="#" onClick="go()">
<iframe src="testcode2.html" />
</div>
testcode2.html
is as follows:
<a href="http://translate.google.">
<img src="http://cdn01.cdn.pinkisthenewblog./wp-content/uploads/2012/07/071712_findingnemosequelfeat-250x250.jpg"/></a>
Share
Improve this question
edited Feb 3, 2014 at 6:13
user3264549
asked Feb 3, 2014 at 5:53
user3264549user3264549
211 silver badge3 bronze badges
2
- is testcode2.html on the same domain as your main html ? – Madhur Commented Feb 3, 2014 at 6:15
- no. they are different domain each other – user3264549 Commented Feb 3, 2014 at 6:22
5 Answers
Reset to default 3This might help you http://jsfiddle/fjGJ5/
jQuery
$(document).ready(function(){
$(".frame").on('click', call);
});
function call(){
alert("I am called.");
}
HTML
<div class="frame">
<iframe src="http://www.jsfiddle/" name="iframe_a"></iframe>
</div>
Give the div an ID and then fire the onClick event using jQuery.
<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>
Move your script on testcode2.html
<a href="javascript:go()">
<img src="http://cdn01.cdn.pinkisthenewblog./wp-content/uploads/2012/07/071712_findingnemosequelfeat-250x250.jpg"/></a>
Then make the redirect by javascript after alert
To call a Javascript function defined in parent use window.parent
<a href="http://translate.google." onclick="window.parent.go();">
<img src="http://cdn01.cdn.pinkisthenewblog./wp-content/uploads/2012/07/071712_findingnemosequelfeat-250x250.jpg"/>
</a>
You might want to stop the redirect in which case add a return false
to your go() function and then use onclick="return window.parent.go();"
<script>
window.onload = function() {
document.getElementById("test_frame").contentWindow.document.body.onclick = function(){
alert("Hello World!");
};
};
</script>
<iframe id="test_frame" src="testcode2.html" />
EDIT: If you cannot use getElementById(), querySelector() or getElementsByTagName() may work.
<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" />
本文标签: javascriptHow to working onclick event on div including iframeStack Overflow
版权声明:本文标题:javascript - How to working onclick event on div including iframe? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744074648a2586519.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论