Javascript 如何使用jquery访问父窗口对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2167455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:03:05  来源:igfitidea点击:

how to access parent window object using jquery?

javascriptjquery

提问by Vicky

How to acess parent window object using jquery?

如何使用jquery访问父窗口对象?

This is my Parent window variable , I want to set its value after closing child window .

这是我的父窗口变量,我想在关闭子窗口后设置它的值。

$('#serverMsg').html('some text here');

回答by roman

window.opener.$("#serverMsg")

回答by Pekka

If you are in a po-up and you want to access the opening window, use window.opener. The easiest would be if you could load JQuery in the parent window as well:

如果您在弹出窗口中并且想要访问打开的窗口,请使用window.opener. 最简单的方法是,如果您也可以在父窗口中加载 JQuery:

window.opener.$("#serverMsg").html// this uses JQuery in the parent window

window.opener.$("#serverMsg").html// 这在父窗口中使用 JQuery

or you could use plain old document.getElementByIdto get the element, and then extend it using the jquery in your child window. The following shouldwork (I haven't tested it, though):

或者你可以使用普通的 olddocument.getElementById来获取元素,然后在你的子窗口中使用 jquery 扩展它。以下应该有效(不过我还没有测试过):

element = window.opener.document.getElementById("serverMsg");
element = $(element);

If you are in an iframe or frameset and want to access the parent frame, use window.parentinstead of window.opener.

如果您在 iframe 或框架集中并且想要访问父框架,请使用window.parent代替window.opener

According to the Same Origin Policy, all this works effortlessly only if both the child and the parent window are in the same domain.

根据同源策略,只有当子窗口和父窗口都在同一个域中时,所有这些才能毫不费力地工作。

回答by kajo

or you can use another approach:

或者您可以使用另一种方法:

$( "#serverMsg", window.opener.document )

回答by John Langford

Here is a more literal answer (parent window as opposed to opener) to the original question that can be used within an iframe, assuming the domain name in the iframe matches that of the parent window:

这是对可以在 iframe 中使用的原始问题的更直接的答案(父窗口而不是 opener),假设 iframe 中的域名与父窗口的域名匹配:

window.parent.$("#serverMsg")