【问题标题】:Javascript for Previous and Next Buttons上一个和下一个按钮的 Javascript
【发布时间】:2015-02-22 06:44:53
【问题描述】:

我正在尝试编写一个函数,该函数将使用一组名为 1.html、2.html、3.html、4.html 等的 html 页面,每个页面上都有上一个和下一个按钮。单击上一个或下一个箭头按钮将带我到浏览器中的下一个 html 页面。所以如果我在一个名为“2.html”的页面上,它会知道,并带我到“3.html”

我看到很多分页脚本,但这要简单得多。我想实际转到另一个 html 页面。我有一些这样的代码,但它没有像我预期的那样工作——谁能帮忙...?

  function nextPg(step) { 
      var str = window.location.href;
      if(pNum = str.match(/(\d+)\.html/i)){
        pNum = pNum[1] * 1 + step+'';
        if(pNum>0){
          pNum = "".substr(0, 4-pNum.length)+pNum;
          window.location = str.replace(/\d+\.html/i, pNum+'.html'); 
        }
      }
    } 

<a href="nextPg(+1)">Next Page &raquo;</a>

我尝试了@void 的建议并像这样编辑了代码,但仍然无法让它工作以加载我的名为“page2.html”的下一页

$( "#target" ).keyup(function( event ) {
  
    function newDoc(){
    var url = window.location.href;
    url = url.split('/').pop();
    curr = +curr.replace("page","").replace(".html","");
    window.location.href = "page"+ (curr+1) +".html";
    }
})
 
$( "#other").click(function() {
  $( "#target" ).keyup();
});

我的按钮如下所示:

&lt;button id="other"&gt;LOAD ANOTHER PAGE&lt;/button&gt;

【问题讨论】:

  • 为什么不使用cookies?

标签: javascript jquery jquery-pagination


【解决方案1】:

为什么不选择简单的方法?

  function nextPg(){
    var url = window.location.href;
    url = url.split('/').pop();
    curr = +curr.replace("page","").replace(".html","");
    window.location.href = "page"+ (curr+1) +".html";
    }

现在根据需要将它与 keyup 事件绑定。

【讨论】:

  • 这看起来像是更好的代码,我试过了,但我还需要什么其他的东西才能让它工作吗?我如何将它与 keyup 事件绑定?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 2013-12-14
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多