[英]how to trigger a click event from within an iframe to an element of its parent?
How can I trigger a click event from within an iframe to an element of its parent? 如何从iframe中触发单击事件到其父元素?
I tried the following code on the parent page before I open the iframe. 在打开iframe之前,我在父页面上尝试了以下代码。
jQuery('iframe').contents().bind('eventhandler', function() {
alert('event was fired');
});
I also tried 我也试过了
jQuery(document).bind('eventhandler', function(e) {
alert('event was fired');
});
and within the iframe I have embeded this code in a click event 在iframe中,我将此代码嵌入到click事件中
jQuery(document).trigger('eventhandler');
but it does't work am I doing something wrong here? 但它不起作用我在这里做错了吗?
You need to refer to the parent document from the iframe. 您需要从iframe引用父文档。 This should do what you need: 这应该做你需要的:
index.html: index.html的:
<iframe src="iframe.html"></iframe>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function(){
$(document).on('eventhandler', function() {
alert('event was fired');
});
});
</script>
iframe.html: Iframe.html的:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function(){
parent.$(parent.document).trigger('eventhandler');
});
</script>
If you have control of the source for both, you can do it like this 如果你可以控制两者的来源,你可以这样做
How to communicate between iframe and the parent site? 如何在iframe和父网站之间进行通信?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.