gpt4 book ai didi

javascript - 打开和关闭带有关闭装置的 window

转载 作者:行者123 更新时间:2023-11-28 06:26:03 25 4
gpt4 key购买 nike

我想通过单击两个按钮来打开和关闭一个窗口。打开按钮打开窗口,关闭按钮关闭窗口。简单!

我通过两个独立函数的帮助实现了这一点。你们猜对了。一个用于打开,另一个用于关闭。

但是,这涉及将 myWindow 对象设为全局。为了避免这种情况,我在想闭包是否可以派上用场,将 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>

最佳答案

工作副本来了

<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>

有哪些变化?我没有返回一个对象,而是将 close window 分配给一个全局变量,这创建了 openWindow 函数的闭包。

windowCenter函数中,myWindow不是一个新变量,而是来自openWindow上下文的变量。我没有从 windowCenter 函数返回任何内容

openWindow 上下文中删除了 publicClose,并在全局上下文中添加了 publicClose 函数。但这是可以避免的,并且可以通过单击按钮直接调用 closeWindow

关于javascript - 打开和关闭带有关闭装置的 window ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35126009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com