Javascript 如何将单击事件添加到iframe

Javascript 如何将单击事件添加到iframe,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,我在页面上有一个iframe(在其中使用广告)。我正在尝试,当用户访问他在网页上单击的任何地方时,它都会在新选项卡中打开此广告,就像123movies.to一样。我也会在30分钟后用cookies来显示主div*注意:我使用div绿色和block,iframe opacity=1进行测试。比如: =----JQUERY----= $(文档).ready(函数(){ $(“#父项_弹出窗口”)。单击(函数(){ var$iframe=$(“#iframe1”).contents(); $ifra

我在页面上有一个iframe(在其中使用广告)。我正在尝试,当用户访问他在网页上单击的任何地方时,它都会在新选项卡中打开此广告,就像123movies.to一样。我也会在30分钟后用cookies来显示主div*注意:我使用div绿色和block,iframe opacity=1进行测试。比如:

=----JQUERY----=


$(文档).ready(函数(){
$(“#父项_弹出窗口”)。单击(函数(){
var$iframe=$(“#iframe1”).contents();
$iframe.click();
});
});
if(document.cookie.indexOf(“已访问”)>=0){
}否则{
var延迟=500;
setTimeout(“document.getElementById('parent_popup').style.display='block',delay_popup”);
到期日=新日期();
expiry.setTime(expiry.getTime()+(30*10*1000));//30分钟
document.cookie=“visited=yes;expires=“+expire.togmString();
}
=----CSS----=


#父菜单弹出窗口{
背景:绿色;
光标:指针;
显示:无;
位置:固定;
z指数:99999;
排名:0;
右:0;
底部:0;
左:0;
宽度:100%;
高度:100%;}
=----HTML----=


我想,这个答案可以帮助你


如果没有,请创建一个示例,我可以在其中进行测试。

您不能。与iframe相关的事件发生在iframe创建的不同窗口中,不会传播到父页面窗口。如果iframe来自不同的域,则无法在其内部访问
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#parent_popup').click(function(){
var $iframe = $("#iframe1").contents();
$iframe.click();   
});
});
</script>


<script language="javascript">
if (document.cookie.indexOf("visited") >= 0) {
} else {
var delay_popup = 500;
setTimeout("document.getElementById('parent_popup').style.display='block'", delay_popup);
expiry = new Date();
expiry.setTime(expiry.getTime()+(30*10*1000)); // 30 minutes
document.cookie = "visited=yes; expires=" + expiry.toGMTString();
}
</script>
<style>
#parent_popup {
background: green;
cursor:pointer;
display: none;
position: fixed;
z-index: 99999;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;}
</style>
<div id="parent_popup">
<iframe id="iframe1" style="opacity:1; filter: alpha(opacity=0)" marginwidth="0" marginheight="0" src="http://webpage.com/advertisement.html" frameborder="" height="100%" scrolling="NO" width="100%"></iframe>
</div>