<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>close()</title>
<script type="text/javascript">
var mywin;
function openWindow(){
mywin=window.open("http://www.imooc.com",'_blank');
}
function closeWindow(){
mywin.close();
}
</script>
</head>
<body>
<form>
<input type="button" value="新建窗口" onclick="closeWindow()" />
<input type="button" value="关闭窗口" onclick="closeWindow()" />
</form>
</body>
</html>
在函数外声明的变量相当于全局变量了 两个函数都可以使用
你把第一个按钮中的“closewindow()”函数替换成openwindow()就可以了
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>close()</title> <script type="text/javascript"> var mywin; function openWindow(){ mywin=window.open("http://www.imooc.com",'_blank'); } function closeWindow(){ mywin.close(); } </script> </head> <body> <form> <input type="button" value="新建窗口" onclick="openWindow()" /> <input type="button" value="关闭窗口" onclick="closeWindow()" /> </form> </body> </html>
<input type="button" value="新建窗口" onclick="closeWindow()" />应该是openWindow();
关闭窗口:window.close();