如何将事件绑定到本地iframe中的元素? [英] How can i bind an event to an element inside a local iframe?
问题描述
首先,父页面和iframe都托管在同一台服务器上(我在WAMP上的localhost),因此这里的原始策略不应该是一个问题。
Firstly both the parent page and the iframe are hosted on the same server (my localhost on WAMP) so same origin policy should not be an issue here.
I无法让触发器工作。我的iframe的ID为 iframe
。
I can't get the trigger to work. My iframe has the id of iframe
.
$(window).load(function(){
//iframe ad hovers
$('#iframe').contents().find('body div').click(function(){
alert('do something here');
});
});
我做错了什么?
推荐答案
我不确定浏览器是否会从< iframe>
转到包含窗口。加载到< iframe>
的文档是否有自己的jQuery副本?如果是这样,你可以试试这个:
I'm not sure the browser's going to propagate a "click" event from the window context of the <iframe>
out to the containing window. Does the document loaded into the <iframe>
have its own copy of jQuery? If so, you can try this:
$('#iframe').contents().$.find('body div').click(function(){
alert('do something here);
});
该更改使< iframe>中的jQuery代码成为
窗口处理事件。
我觉得@jAndy是对的,应该按原样运行 - 但您必须确保加载框架中的文档。试试这个:
Well I think that @jAndy is right and that should work as is - but you have to make sure the document in the frame is loaded. Try this:
$('#iframe').load(function() {
$(this).contents().find('body div').click(function() { alert("hi"); });
});
这篇关于如何将事件绑定到本地iframe中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!