gpt4 book ai didi

javascript - 单击单个按钮即可在 2 个 HTML 文件之间切换的 jQuery

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

我是编程新手,所以请耐心等待。我有一个按钮,当您单击它时,它将转到另一个 HTML 文件,但是当我再次单击它时,它不会返回到之前所在的另一个 HTML 文件。本质上我想在两者之间切换(或者如果我添加其他页面则更多)。

尝试仅使用 jQuery 来完成此操作。

$(".button").on("click", function() {
if(window.location.href = "index1.html"){
window.location.replace("index2.html");
}

});

$(".button").on("click", function () {
if (window.location.href = "index2.html") {
window.location.replace("index1.html");
}
});

对于这样的任务来说,这似乎过于复杂。任何帮助将不胜感激!

最佳答案

如果这两个代码块位于同一文件中,则第二个 onclick 事件将覆盖第一个。如果您只需要重定向,window.location 也可能就足够了。

试试这个:

$(".button").on("click", function() {
if(window.location.href == "index1.html"){
window.location="index2.html";
}else if(window.location.href == "index2.html"){
window.location="index1.html";
}
});

关于javascript - 单击单个按钮即可在 2 个 HTML 文件之间切换的 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47193297/

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