jQuery 从父窗口关闭子窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15125577/
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
Closing child window from parent window
提问by user2014429
I cant figure out why this won't work. Is there something wrong with this code? The function is being called I checked with an alert but it just won't close the window.
我不明白为什么这行不通。这段代码有什么问题吗?正在调用该函数我检查了警报,但它不会关闭窗口。
$('#click').click(function() {
var win = window.open("test3.html","something","width=550,height=170");
});
function closeit(){
win.close();
}
and on test3.html
并在 test3.html 上
window.opener.closeit();
回答by Ry-
Your win
variable is scoped to the function that handles the click event. Put it in a scope shared by both that function and closeit
.
您的win
变量范围为处理点击事件的函数。将其放在该函数和closeit
.
In this case, that would probably look like:
在这种情况下,它可能看起来像:
var win;
…
$('#click').click(function() {
win = window.open("test3.html", "something", "width=550,height=170");
});
});
function closeit() {
win.close();
}
回答by mgibsonbr
The first win
is a local variable in the callback scope, while the second is a global object. Move the definition (var win
) to outside the function and it should work.
第一个win
是回调范围内的局部变量,而第二个是全局对象。将定义 ( var win
) 移到函数之外,它应该可以工作。