从iframe中触发的事件是否可以由其父元素处理? [英] Can events fired from an iframe be handled by elements in its parent?
问题描述
假设我有一个位于 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屋!