【问题标题】:How to link to next and previous pages in a sequence with no PHP (just jQuery/javascript)如何在没有 PHP 的情况下按顺序链接到下一页和上一页(仅 jQuery/javascript)
【发布时间】:2014-10-26 05:09:54
【问题描述】:

假设我有一系列页面(“example_1.html”、“example_2.html”、“example_3.html”等),并且我希望每个页面都有下一个/上一个按钮。我不想要任何过于花哨的分页,只需要 prev 和 next 按钮。

我还希望这些页面在到达结尾后循环回来,这样“example_12.html”自然会循环回到开头的“example_1.html”。

我希望能够通过创建另一个页面来简单地将页面添加到序列中,而无需返回现有页面来更改链接。

有没有一种简单的方法可以在没有 php 或服务器端数据获取的情况下做到这一点?此外,我不想随着时间的推移添加更多页面而不断更新列表,并且我不想在页面中包含任何列表

我总能找到一些接近的东西,但从来没有我理解的东西,是否有一个真正简化的版本供不精通 javascript 的人使用?

【问题讨论】:

  • 是否有可供 JavaScript 提取的 HTML 结构?作为参考的主要链接列表
  • @Brian 理想情况下不,我不想随着时间的推移添加更多页面而不断更新列表,并且我不想在页面中包含任何列表。
  • 这可能是个问题,如果没有这两个选项中的任何一个。脚本将无法确定下一个或上一个链接是什么,或者实际上何时必须返回到开头。 . 的不存在的列表:)

标签: javascript jquery html hyperlink next


【解决方案1】:

如果您真的不能使用服务器端语言(也许如果您希望静态托管它),并且不希望手动设置链接的麻烦,我建议您使用静态站点生成器喜欢Jekyll。如果你确定你想要一个 JS 解决方案,这应该可以工作:

// Get current page url, remove the `.html` extension and split by underscore.
// The current page number will be the last element in the returned array.
var urlFrags = window.location.href.replace(".html", "").split("_"),
    // Get the last element of the `urlFrags` array and parse. This should be the current page number.
    curPage = parseInt(urlFrags[urlFrags.length - 1]),
    // Form the url of the expected next page. (Current page number + 1.)
    nextPage = "example_" + (curPage + 1) + ".html";

// Check if a page on this domain exists.
// Success returns true, error returns false.
function pageExists(url, callback){
    $.ajax({
        url: url,
        type: "HEAD",
        success: function(){ callback(true); },
        error: function(){ callback(false); }
    });
}

// Setup links for later.
var backLink, nextLink;

// Check if expected next page exists.
pageExists(nextPage, function(exists){
    if(exists){
        // If it does, set nextlink as expected next page.
        nextLink = nextPage;
    } else {
        // Otherwise, loop back to the start.
        nextLink = "example_1.html";
    }
});

if(curPage > 1){
    // We know the previous page must exist, so long as you're not on the first page.
    backLink = "example_" + (curPage - 1) + ".html";

    // It's not practical to try and loop back to the end if you go back on the first page.
    // You'd have to query pages until you got an error. So just leave it undefined, and ideally remove the button.
}

// Example button settings, using jQuery. Replace with whatever you're using.
$("#back").prop("href", backLink);
$("#next").prop("href", nextLink);

如果不设置相同的站点,显然无法测试所有这些,因此请原谅任何错误;)

【讨论】:

  • 这个解决方案正是我想要的!我不知道如何拆分名称然后将一个添加到系列中,非常感谢!但我无法让它工作,链接只是转到页面/不要转到请求的页面
  • 你所有的example_*.html文件都在同一个目录下吗?我提供的示例按钮设置仅供展示,您必须自己替换它们并绑定链接。如果您发布您的 HTML,我可以为您提供帮助。
  • 仅供参考,curPage = parseInt(urlFrags[urlFrags.length-1]),这是唯一的错误,非常感谢!
  • 你认真地完成了我的整个一周;我希望我能做更多的事情来报答你,哈哈。如果您需要任何设计/艺术品,请联系我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 2014-09-24
  • 2022-11-14
  • 2022-01-25
  • 2014-12-14
  • 2011-12-23
  • 1970-01-01
相关资源
最近更新 更多