简体   繁体   English

将事件从iframe传递到父级

[英]Passing events from iframe to parent

I try to load an iframe which inside I trigger my custom events, those events need to be captured in a parent DOM and activate functions there. 我尝试加载一个iframe,在其中触发我的自定义事件,这些事件需要在父DOM中捕获并在那里激活功能。

I tried to look for different approaches but none worked, I know I can call parent.postMessage(), but the issue is I dont know how many parents (generations) I have. 我试图寻找不同的方法,但没有一种有效,我知道我可以调用parent.postMessage(),但是问题是我不知道我有多少个父母(世代)。

I would like to just call postMessage(), and it will propogate to all parents and captured on those who listen to the event. 我只想调用postMessage(),它将传播给所有父母,并在听事件的人中捕获。

Is this possible at all? 这有可能吗?

Been a while since I've had to do anything like this, but I think you'd need to control this the other way around, so trigger the events at window scope within the iframe, and handle the events within the parent frame. 自从我不得不做这样的事情以来已经有一段时间了,但是我认为您需要以其他方式进行控制,因此在iframe中的窗口范围内触发事件,并在父框架中处理事件。

//Inside iFrame
$(window).trigger('myevent', { a: 'b' });

//In parent document
$('#myIframe').on('myevent', function(e) {
    //Handling the event.
});

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

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