有一段时间没有用 JS 编程了。 现在,我有以下事情:
<html>
<head>
<script language="JavaScript">
function enlarge()
{
window.close();
}
</script>
</head>
<body>
<img src="addresstomyimg.png" onClick="enlarge()" />
</body>
</html>
(它非常简单,因为事实上,我有带有自定义 JS 修改等的 WordPress 平台,但总的来说,就是这个想法)。
我不明白为什么它不起作用。
最佳答案
JavaScript 无法关闭窗口,除非它打开了窗口。
在您的函数中,将 window.close()
替换为 alert('here')
,您将看到该函数运行正常。
如果你想让你的函数关闭一个窗口,首先打开一个:
<html>
<head>
<script type="text/javascript">
var popup;
function closewin()
{
popup.close();
}
function openwin()
{
popup = window.open('http://www.google.com');
}
</script>
</head>
<body>
<img src="addresstomyimg.png" onclick="openwin()" /> Click to open, then come back here
<br><br>
<img src="addresstomyimg.png" onclick="closewin()" /> Click to close
</body>
</html>
关于javascript - 如何使用 Javascript 关闭窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9181714/