使用 jQuery 定位父窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18372746/
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-26 21:27:39  来源:igfitidea点击:

targeting the parent window using jQuery

javascriptjqueryjquery-selectors

提问by naresh kumar

Here is the sample code which I am not able to solve. I did it using javascript, but when I am doing using jQuery, I do not able to target the element.

这是我无法解决的示例代码。我是使用 javascript 完成的,但是当我使用 jQuery 时,我无法定位该元素。

Script :

脚本 :

var element = window.parent.document.getElementById('iframeOne');
//this is working fine      

But i want to do using jQuery. So how can I target the element?

但我想使用 jQuery。那么如何定位元素呢?

回答by bhb

Perhaps you want to do something like this

也许你想做这样的事情

$('#iframeOne', window.parent.document);

Another way to do it

另一种方法来做到这一点

window.parent.$("#iframeOne");

Another way

其它的办法

$("#iframeOne", top.document);

If you know the name of the parent window, you can also do

如果你知道父窗口的名称,你也可以这样做

$("#iframeOne",opener.document)

Here openeris the name of the window.

opener是窗口的名称。

Cheers!!

干杯!!

回答by Tushar Gupta - curioustushar

to select element with id within the parent window

在父窗口中选择带有 id 的元素

$('#iframeOne',window.parent.document);

回答by palerdot

The jQuery selector syntax for id is to use a # before the id name

id 的 jQuery 选择器语法是在 id 名称前使用 #

in you case it should be $('#iframeOne')

在你的情况下应该是 $('#iframeOne')

an optional context can also be used like $('#iframeOne, window.parent.document). The default context is document root.

也可以像$('#iframeOne, window.parent.document). 默认上下文是文档根。

回答by Ashwani

Use this:

用这个:

var ele = $('#iframeOne', window.parent.document);

or

或者

var ele = $(window.parent.document).find("#iframeOne");