上一个和下一个按钮的Javascript

Javascript for Previous and Next Buttons

本文关键字:Javascript 按钮 下一个 上一个      更新时间:2024-03-08

我正在尝试编写一个函数,该函数将使用一组名为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();
});

我的按钮看起来像这样:

<button id="other">LOAD ANOTHER PAGE</button>

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

  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事件绑定。