javascript jquery按名称获取窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11833403/
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
jquery get window by name
提问by pillarOfLight
Possible Duplicate:
Access a window by window name
可能重复:
按窗口名称访问窗口
so suppose I do
所以假设我这样做
window.open(url, 'somewindowname');
how can I get a reference to the newly opened window using the window name ('somewindowname') using jquery?
如何使用 jquery 使用窗口名称('somewindowname')获得对新打开的窗口的引用?
Perhaps something in the form of $('somewindowname') or something....
也许是 $('somewindowname') 形式的东西……
回答by Eonasdan
you can set the window as a variable like so
您可以像这样将窗口设置为变量
var foo;
foo = window.open(url, 'somewindowname');
you can then
然后你可以
use
利用
foo.close();
to close the window for instance. Note that this is a javascript variable not a jquery variable.
例如关闭窗口。请注意,这是一个 javascript 变量而不是 jquery 变量。
回答by fardjad
It has nothing to do with JQuery
.
它与JQuery
.
After opening the window for the first time, you can get a reference to it by using window.open
function again and assigning the returned value to a variable:
第一次打开窗口后,可以通过window.open
再次使用函数并将返回值赋给变量来获取对它的引用:
var existingWindow = window.open(url, "somewindowname");
Edit
编辑
Here is a JSFiddle
demo.
这是一个JSFiddle
演示。
回答by epascarello
Reference elements inside of the pop up window:
弹出窗口内的参考元素:
var imgsInPopUp = $("img", somewindowname.document.body);
回答by Juan Mendes
There is some undocumented behavior that lets you get a reference to a window by name. I've mentioned it in this answer How to tell if a window exists in Javascript?
有一些未记录的行为可让您通过名称获取对窗口的引用。我在这个答案中提到过如何判断 Javascript 中是否存在窗口?
// Opened a window without storing a handle, but gave it a name
window.open('/some/url', 'xxx');
// Now I need to get a reference to that window
// Calling open without setting a url gets you
// a reference and doesn't reload the window
var win = window.open('', 'xxx')
Here's an example
这是一个例子