繁体   English   中英

在 iframe 的父窗口中触发事件

[英]Trigger events in iframe's parent window

为什么以下不起作用:

//iframe:
window.parent.$(document).trigger('complete');

//parent window:
$(document).bind('complete', function(){
  alert('Complete');
});

虽然以下是工作:

//iframe:
window.parent.$('body').trigger('complete');

//parent window:
$('body').bind('complete', function(){
  alert('Complete');
});

?

事件跟踪的方式,您只能在同一文档上触发或接收事件。

试试

window.parent.$(window.parent.document).trigger('complete');

您可以尝试在父文档中添加一个触发函数,然后从 iframe 中将其作为常规函数调用。 这应该确保您在正确的文档上下文中触发事件。

// In Parent
function triggerComplete () {
  $(document).trigger('complete');
}

// In iFrame
window.parent.triggerComplete();

直接回答 OP 的问题:因为第一个代码示例中存在错误。

您将 iframe 的文档对象传递给父级的$函数。 这没有多大意义。

  1. Parent 的 jQuery 实例无法在其他窗口的 DOM 对象上触发事件;

  2. 即使可以,它也是 iframe 的文档,但您尝试侦听父文档上的事件。

这可能会起作用:

window.parent.$(window.parent.document).trigger('complete');

检查这个解决方案,它非常棘手:

top.frames['frame_name'].document.getElementById('Processing').style.display = 'none'; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM