gpt4 book ai didi

javascript - 如何关闭通过单击 JavaScript 中的图像打开的新选项卡?

转载 作者:行者123 更新时间:2023-12-03 00:41:48 28 4
gpt4 key购买 nike

我用 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 - 如何关闭通过单击 JavaScript 中的图像打开的新选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53425068/

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