在 JavaScript 中检查数组中的值?

发布于 2024-12-26 05:01:59 字数 367 浏览 2 评论 0原文

我有一个:

var pageURL = location.href; // stores the URL of the current page in the var pageURL

我有:

var Page = Array ();
page [0] = "http://web1.com"
page [1] = "http://web2.com"
page [2] = "http://web3.com"

现在我想做一个函数(称为 nextPage)来检查数组中的哪一个页面等于 PageURL。我还需要一个按钮,所以当我单击它时,它将带我进入下一页。这意味着我想将页面增加 1。

I have a:

var pageURL = location.href; // stores the URL of the current page in the var pageURL

And I have :

var Page = Array ();
page [0] = "http://web1.com"
page [1] = "http://web2.com"
page [2] = "http://web3.com"

Now I want to make a function (called nextPage) to check which one of these pages in the array equals the PageURL. Also I need to have a button, so when I click it it will take me to the next page. What that mean I want to increment the page by 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜嗑 2025-01-02 05:01:59

您可以非常简单地使用

var current = page.indexOf(location.href);

Then 转到下一页

location.href = page[current + 1];

You can use very simply

var current = page.indexOf(location.href);

Then to go to the next page

location.href = page[current + 1];
凉月流沐 2025-01-02 05:01:59
    var form = document.createElement('form');
    form.id = "nextForm";
    document.body.appendChild(form);

    var nextButton = document.createElement('input');
    nextButton.type = 'button';
    nextButton.id = 'nextButton';
    nextButton.value = 'Next Button';
    nextButton.onclick = nextPage;
    form.appendChild(nextButton);

    function nextPage() {



        var page = Array ();
        page[0] = "http://web1.com" // insert your urls here
        page[1] = "http://web2.com"
        page[2] = "http://web3.com"


        var matchIndex = page.indexOf(location.href);
        var nextIndex;
        if (matchIndex !== -1 && page[matchIndex + 1]) {
            nextIndex = matchIndex + 1;
        } else {
            alert("You're at the last page"); // next page in the array does not exist, you can handle the error however you want
            return;
        }

        goToNextPage(page[nextIndex]);
    }
    function goToNextPage(url) {
        document.location.href=url;
    }
    var form = document.createElement('form');
    form.id = "nextForm";
    document.body.appendChild(form);

    var nextButton = document.createElement('input');
    nextButton.type = 'button';
    nextButton.id = 'nextButton';
    nextButton.value = 'Next Button';
    nextButton.onclick = nextPage;
    form.appendChild(nextButton);

    function nextPage() {



        var page = Array ();
        page[0] = "http://web1.com" // insert your urls here
        page[1] = "http://web2.com"
        page[2] = "http://web3.com"


        var matchIndex = page.indexOf(location.href);
        var nextIndex;
        if (matchIndex !== -1 && page[matchIndex + 1]) {
            nextIndex = matchIndex + 1;
        } else {
            alert("You're at the last page"); // next page in the array does not exist, you can handle the error however you want
            return;
        }

        goToNextPage(page[nextIndex]);
    }
    function goToNextPage(url) {
        document.location.href=url;
    }
下壹個目標 2025-01-02 05:01:59

根据 Joseph 的回答,完整的代码将如下所示:

function goToNextPage() {
    var currentPageUrl = location.href;

    var pageUrls = [
        "http://web1.com",
        "http://web2.com",
        "http://web3.com"
        ];

    var currentIndex = pageUrls.indexOf(location.href);
    var nextIndex = currentIndex + 1;
    var nextPageUrl = pageUrls[nextIndex];
    location.href = nextPageUrl;
}

另外,如果 IE 中对 indexOf 的支持对您来说是一个问题,请查看另一个 StackOverflow 问题的答案: 如何在 Internet Explorer 浏览器的 JavaScript 中修复 Array indexOf()

Based on Joseph's answer the full code would look something like this:

function goToNextPage() {
    var currentPageUrl = location.href;

    var pageUrls = [
        "http://web1.com",
        "http://web2.com",
        "http://web3.com"
        ];

    var currentIndex = pageUrls.indexOf(location.href);
    var nextIndex = currentIndex + 1;
    var nextPageUrl = pageUrls[nextIndex];
    location.href = nextPageUrl;
}

Also if support for indexOf is an issue for you in IE check out the answer to that from another StackOverflow question: How to fix Array indexOf() in JavaScript for Internet Explorer browsers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文