从 iframe 触发的事件可以由其父元素中的元素处理吗? [英] Can events fired from an iframe be handled by elements in its parent?
问题描述
假设我有一个位于 www.example.com/foo
的页面,它包含一个 和
src="http://www.example.com/bar"
.我希望能够从 /bar
触发一个事件,并让 /foo
听到它.使用 Prototype 库,我尝试执行以下操作但没有成功:
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:
节点不能在创建它的文档以外的文档中使用"代码: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屋!