gpt4 book ai didi

javascript - 链接仅在第一次点击时有效,从第二次点击开始不会重定向到 href 中指定的页面

转载 作者:行者123 更新时间:2023-11-30 16:38:05 25 4
gpt4 key购买 nike

我希望此链接仅在第一次点击时有效。从第二次点击开始,它不会重定向到 a.php,并且会在点击超链接后停留在同一页面上。

<script>
var j=0;
function a()
{
if(j==0)
{
window.location.href="a.php";
j=1;
}
else
{
window.location.href="t.html";
}

}
</script>
<a href="a.php" onclick="a()">hi</a>

最佳答案

@Okx 所说的非常正确,但是使用 localStorage 在从 IE8 开始的每个浏览器上都可以正常工作。

function a() {
var count = parseInt(localStorage.count_of_a, 10);
if (isNaN(count)) count = 0;
if (count == 0) {
count += 1;
localStorage.count_of_a = count;
window.location = 'a.php';
} else {
window.location = 't.html';
}
}
// Note 1: this won't work if Javascript is disabled!
// Note 2: people can reset their local storage and rewrite the variables
// themselves!

关于javascript - 链接仅在第一次点击时有效,从第二次点击开始不会重定向到 href 中指定的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32415225/

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