javascript - 如何关闭通过单击javascript中的图像打开的新标签?
问题描述
我已经在 html 中编写了一个代码,该代码将在单击图像后在新选项卡中打开一个链接,但问题是我无法关闭该新窗口。
我想知道如何关闭那个新窗口??
<html>
<h1>Click on the image below to open google homepage </h1>
<head>
<script>
var x;
var win = document.links;
function closeFunc() {
win[0].close();
}
</script>
</head>
<body>
<a id="google" href="http://www.google.com" target="_blank">
<img src="https://usercontent1.hubstatic.com/12498898_f520.jpg" alt="image" />
</a>
<br> <br>
<button onclick="closeFunc()"> Close</button>
</body>
</html>
解决方案
试试这个
<html>
<h1>Click on the image below to open google homepage </h1>
<head>
<script>
var x;
var win = document.links;
function closeFunc() {
win[0].close();
}
function popuponclick()
{
my_window = window.open("http://www.google.com",
"mywindow");
}
function closepopup()
{
if(false == my_window.closed)
{
my_window.close ();
}
else
{
alert('Window already closed!');
}
}
</script>
</head>
<body>
<a id="google" href="javascript: popuponclick()" target="_blank">
<img src="https://usercontent1.hubstatic.com/12498898_f520.jpg" alt="image" />
</a>
<br> <br>
<button onclick="javascript: closepopup()"> Close</button>
</body>
</html>
推荐阅读
- javascript - 我的 venobox jQuery 视频帧未加载
- ios - UIView isFocused 与 isFirstResponder
- traveling-salesman - 具有多次行程的 OR-Tools CVRP
- hashicorp-vault - 如何编写允许在 Hashicorp Vault 中删除策略的策略
- java - 带有数据提供者的 JUnit TestNG,其中包括一个匿名函数
- reactjs - React,Material-UI - onClick() 总是将最后一项作为参数
- javascript - 需要最佳方法来实现游戏中敌人事件的时间线和迭代间隔
- r - 从 R 3.6 升级到 R 4.0 时,如何将 R 指向正确版本的 curl?
- powerbi - Power BI - DAX RANKX - 如何考虑切片器?
- postgresql - 安装自定义科目表模块时出现数据库错误