我想打开和关闭一个窗口点击两个按钮。打开按钮,关闭按钮关闭窗口。很简单!
我通过两种不同的功能实现了这一点。你们猜到了。一个是开放的,另一个是关闭的。
然而,这涉及到将myWindow 对象变成全局对象。为了避免这种情况,我想如果闭包可以方便地将对象限定为手动定义的函数。。
我试着做一些类似下面粘贴的代码,但不确定如何让它工作。现在,publicClose还没有定义。
同样,我的重点是避免任何全球宣言。
<html>
<head>
<meta charset="UTF-8">
<title>Day 5-6</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<div class="buttons">
<ul class="list-inline">
<li><button class="btn btn-default" onclick="openWindow()">Open Window</button></li>
<li><button class="btn btn-default" onclick="publicClose()">Close Window</button></li>
</ul>
</div>
<script type="text/javascript">
function openWindow(){
var myWindow = windowCenter("", "", 400, 200 );
function windowCenter(url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
var ScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var ScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + ScreenLeft;
var top = ((height / 2) - (h / 2)) + ScreenTop;
var myWindow = window.open("", "", 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
myWindow.document.write("<strong>I am child window.</strong>")
// Puts focus on the newWindow
if (window.focus) {
myWindow.focus();
}
}
function closeWindow(){
myWindow.close();
}
var publicClose = {
close: closeWindow;
}
return publicClose;
}
</script>
</body>
</html>发布于 2016-02-01 09:11:41
这里来了一份工作副本
<title>Day 5-6</title>
<script type="text/javascript">
var closeWindow;
function openWindow() {
var myWindow;
windowCenter("", "", 400, 200);
function windowCenter(url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
var ScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var ScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + ScreenLeft;
var top = ((height / 2) - (h / 2)) + ScreenTop;
myWindow = window.open("", "", 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
myWindow.document.write("<strong>I am child window.</strong>");
// Puts focus on the newWindow
if (window.focus) {
myWindow.focus();
}
}
closeWindow = function() {
myWindow.close();
}
}
function publicClose() {
closeWindow();
}
</script>
<body>
<div class="buttons">
<ul class="list-inline">
<li>
<button class="btn btn-default" onclick="return openWindow();">Open Window</button>
</li>
<li>
<button class="btn btn-default" onclick="publicClose()">Close Window</button>
</li>
</ul>
</div>
</body>有什么变化?我没有返回一个对象,而是将关闭窗口分配给一个全局变量,该变量创建了一个笨拙的openWindow函数。
在windowCenter函数中,myWindow不是一个新变量,而是来自openWindow上下文的变量。我也不会从windowCenter函数返回任何东西
从publicClose上下文中删除openWindow,并在全局上下文中添加publicClose函数。但这是可以避免的,可以直接从按钮单击调用closeWindow。
发布于 2016-02-01 08:49:05
如果您想拥有publicClose全局,就必须定义它为全局的;-)
var publicClose = {};
function openWindow(){
// --- your code from above here ---
publicClose = {
close: closeWindow;
}
}此外,您的关闭函数调用是错误的。固定:
<li><button class="btn btn-default" onclick="publicClose.close()">Close Window</button></li>https://stackoverflow.com/questions/35126009
复制相似问题