jQuery单击单个按钮在2个HTML文件之间切换

时间:2017-11-09 03:15:32

标签: javascript jquery

我是编程新手所以请耐心等待。我有一个按钮,当你点击它时,它会转到另一个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");
    }
});

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

2 个答案:

答案 0 :(得分:1)

如果这两个代码块在同一个文件中,则第二个package hw4Question2; public class SharePattern { public SharePattern(int input1, int input2, int half)//constructor { } public void findFinalDaySharePoints(int input1, int input2, int half) { System.out.println(input2); if(input1%2 == 0) { for(int i = 1; i <= input1 ; ++i) { if(i<half) { input2 = input2 + 50; System.out.println(input2); } else if(i>half) { input2 = input2 - 25; System.out.println(input2); } } } else { for(int i = 1; i <= input1 ; ++i) { if(i<=half) { input2 = input2 + 50; System.out.println(input2); } else if(i>half) { input2 = input2 - 25; System.out.println(input2); } } System.out.println("The final share value is "+input2); } } } 事件将覆盖第一个。如果您只需要重定向,onclick也可能就足够了。

试试这个:

window.location

答案 1 :(得分:0)

根据您最近的评论澄清您的要求,我相信您希望维护一个页面数组,并使用for循环进入下一页同一事件。

var pages = ['index1.html','index2.html','index3.html','index4.html'];

$(".button").on("click", function() {
    for(var i = 0; i < pages.length; i++){
        if(window.location.pathname == pages[i]){
            if(i == pages.length - 1){
                //at the end, go to first page
                window.location.href=pages[0];
            }
            else{
                window.location.href=pages[i+1];
            }
            break;
        }
    }
});

虽然调试此代码并检查window.location的值 - 如果您有一个长路径,它可能不是您所期望的。如果您希望将此工具用于以下some/path/index1.html之类的网址,则需要将整个相对路径(www.foo.com/some/path/index1.html)添加到数组中。