从 iframe 访问父窗口的元素

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

编辑: 目标 div 位于父页面中,因此我的问题基本上就是能否在 iframe 之外对父页面进行 jQuery 调用。那看起来会怎么样?

编辑2: 这就是我的 jquery 代码现在的样子。它在 iframe 页面中。Div # 目标位于 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();
});
})

我不知道脚本是否处于活动状态,因为表单成功地提交了,但是没有发生任何事情。

编辑3:

现在我尝试从 iframe 中的一个链接调用父页面,但也没有成功:

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

Have the below js inside the iframe and use ajax to submit the form.

$(function(){


$("form").submit(e){
e.preventDefault();


//Use ajax to submit the form
$.ajax({
url: this.action,
data: $(this).serialize(),
success: function(){
window.parent.$("#target").load("urlOfThePageToLoad");
});
});


});


});

I think you can just use window.parent from the iframe. window.parent returns the window object of the parent page, so you could do something like:

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

Then do whatever you want with that div.

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');

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

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

You can access elements of parent window from within an iframe by using window.parent like this:

// using jquery
window.parent.$("#element_id");

Which is the same as:

// pure javascript
window.parent.document.getElementById("element_id");

And if you have more than one nested iframes and you want to access the topmost iframe, then you can use window.top like this:

// using jquery
window.top.$("#element_id");

Which is the same as:

// pure javascript
window.top.document.getElementById("element_id");