从 iframe 访问父窗口的元素 [英] Access elements of parent window from iframe

查看:34
本文介绍了从 iframe 访问父窗口的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,我们可以调用 parent.php.在这个页面中,我有一个带有提交表单的 iframe,除此之外我还有一个 ID 为target"的 div.是否可以在 iframe 中提交表单,并在成功时刷新目标 div.比如说在其中加载一个新页面或其他什么?

I have a page we can call parent.php. In this page I have an iframe with a submit form and outside of that I have a div with the id "target". Is it possible to submit the form in the iframe and when success refresh the target div. Say load a new page into it or something?

目标 div 位于父页面中,所以我的问题基本上是您是否可以在 iframe 之外对父页面进行 jQuery 调用.那会是什么样子?

The target div is in the parent page, so my question is basically if you can make for an example a jQuery call outside the iframe to the parent. And how that would look?

所以这就是我的 jquery 代码现在的样子.它位于 iframe 页面的 .div #target 在 parent.php 中

Edit 2: So this is how my jquery code looks like now. It is in the of the iframe page. the div #target is in the parent.php

$(;"form[name=my_form]").submit(function(e){     
 e.preventDefault; 
 window.parent.document.getElementById('#target'); 
 $("#target").load("mypage.php", function() { 
 $('form[name=my_form]').submit(); 
 }); 
 })

我不知道脚本是否处于活动状态,因为表单提交成功,但目标没有任何反应.

I don't know if the script is active cause the form submits successfully but nothing happens to target.

编辑 3:

现在我正在尝试从 iframe 中的链接调用父页面.那里也没有成功:

Now I'm trying to call the parent page from a link within the iframe. And no success there either:

<a href="javascript:window.parent.getElementById('#target').load('mypage.php');">Link</a>

推荐答案

我认为问题可能是你没有找到你的元素,因为你调用中的#"来获取它:

I think the problem may be that you are not finding your element because of the "#" in your call to get it:

window.parent.document.getElementById('#target'); 

如果您使用 jquery,则只需要 #.这里应该是:

You only need the # if you are using jquery. Here it should be:

window.parent.document.getElementById('target'); 

这篇关于从 iframe 访问父窗口的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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