从iframe中触发的事件是否可以由其父元素处理? [英] Can events fired from an iframe be handled by elements in its parent?

查看:318
本文介绍了从iframe中触发的事件是否可以由其父元素处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个位于 www.example.com/foo 的页面,它包含一个< iframe> src =http://www.example.com/bar。我希望能够从 / bar 发起一个事件,让它被 / foo 听到。使用Prototype库,我尝试了下面的方法,但没有成功:
$ b

Suppose I have a page located at www.example.com/foo, and it contains an <iframe> with src="http://www.example.com/bar". I want to be able to fire an event from /bar and have it be heard by /foo. Using the Prototype library, I've tried doing the following without success:


Element.fire(parent,'ns:frob');

Element.fire(parent, 'ns:frob');

当我这样做时,在Firefox 3.5中,出现以下错误:

When I do this, in Firefox 3.5, I get the following error:


节点不能用于创建文档之外的文档中code:4
第0行

Node cannot be used in a document other than the one in which it was created" code: "4 Line 0

不知道这是否与我的问题有关。是否有一些安全机制阻止 / bar 中的脚本启动 / foo

Not sure if that's related to my problem. Is there some security mechanism that's preventing scripts in /bar from kicking off events in /foo?

推荐答案

我还没有测试过这个跨浏览器,但它在FF中工作。

I haven't tested this cross-browser yet, but it works in FF.

在iFrame中,您可以触发元素parent.document:

In the iFrame you can fire on the element parent.document:

Event.observe(window, 'load', function() {
  parent.document.fire('custom:event');
});

在父框架中,您可以使用它:

and in the parent frame you can catch it with:

document.observe('custom:event', function(event) { alert('gotcha'); });

这篇关于从iframe中触发的事件是否可以由其父元素处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆