将子窗体iframe中的事件附加到父窗口中的处理程序 [英] Attach an event in a child iframe to a handler in the parent window

查看:112
本文介绍了将子窗体iframe中的事件附加到父窗口中的处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有直接访问这个iframe的源代码,所以我想这样做,如果可能这样。

I don't have access to this iframe's source directly, so I would like to do this, if it's possibly this way.

我有一个iframe JS:

I have an iframe generated by JS:

<iframe name="temp_iframe" width="100%" height="95%" src="'+the_url+'"></iframe>

而里面是一个提交按钮和一个取消按钮。提交按钮工作正常,但我想要取消按钮关闭包含iframe的模式...我也希望提交按钮发送,然后关闭模态。通常这将很容易,但我不知道如何在父窗口中设置一个影响子窗口的iframe的子DOM元素。

And inside is a submit button and a cancel button. The submit button works fine, but I want the cancel button to close this modal that is containing the iframe... I also want the submit button to send, then close the modal. Normally this would be easy, but im not sure how to setup an event in the parent window to a child DOM element of the iframe that affects the child's parent, the main window.

EG如果这不是在iframe和jQuery中:

E.g. if this wasn't in an iframe and in jQuery:

$('[name=temp_iframe] button').live('click',function(){
    alert('click');
    return false;
});

编辑:还在同一个域!

推荐答案

使用 contents()访问iframe内的Document对象。请注意,在一个文档中使用jQuery库来操纵另一个文档的一般问题,通常应该避免。然而,在绑定事件的情况下,它的工作正常。

Use contents() to access the Document object inside an iframe. Note that there are in general problems with using a jQuery library in one document to manipulate another document and it should in general be avoided. However, in the case of binding events it does work.

$('[name=temp_iframe]').contents().find('button').click(function() {
    alert('click');
});

这需要加载iframe及其内容,所以在$ code > $(window).load()如果需要的话。因为事件不会从子文档传播到其父文件中,所以不能在文档中生活 / 委托

This requires that the iframe and its contents are loaded, so do it in a $(window).load() handler if necessary. You can't live/delegate across documents, as events don't propagate from a child document into its parent.

这篇关于将子窗体iframe中的事件附加到父窗口中的处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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