从1到10计数并在按钮链接内重复

时间:2017-06-23 15:20:49

标签: javascript php jquery html

我正在寻找一种方法来编写一个从1到10的脚本,在10之后它会重复,在一个buttom链接中。

示例:

<button onclick="window.location.href='/page"1".html'">Submit</button>

首先点击进入&#34; url / page1.html&#34;

再次点击&#34; url / page2.html&#34;

第三到&#34; url / page3.html&#34;

第四到&#34; url / page4.html&#34;

最多十次点击&#34; url / page10.html&#34;

在命中数字10之后,它必须重复到1 agin。

例2:

首先点击进入&#34; url / page1.html&#34;,点击2 =&#34; url / page2.html&#34;,点击3 =&#34; url / page3.html&#34 ;,点击4 =&#34; url / page4.html,点击5 =&#34; url / page5.html,点击5 =&#34; url / page5.html,点击6 =&#34;网址/ page6.html,点击7 =&#34; url / page7.html,点击8 =&#34; url / page8.html,点击9 =&#34; url / page9.html点击10 =&#34;网址/page10.html现在重复点击11 =&#34; url / page1.html,点击12 =&#34; url / page2.html,点击13 =&#34; url / page3.html,点击14 =& #34; URL / page4.html

希望有人可以给我一个很好的例子,说明应该如何运行,更喜欢php或javascript。

4 个答案:

答案 0 :(得分:1)

这个解决方案怎么样:

<button onclick="myScript(10)">Submit</button>

<script>

let currentPage = 1;

function myScript(totalPages) {
    if (currentPage === totalPages) {
        currentPage = 1;
        return window.location.href=`/page10.html`
    }
    currentPage = currentPage < totalPages ? currentPage + 1 : 1;
    return window.location.href=`/page${currentPage}.html`;
}

</script>

我没有对此进行测试,但我相信即使可能存在错误,全局推理对您也有用。我将总页数作为函数参数的事实允许您在其他情况下再次使用它。

你能说出来吗?

答案 1 :(得分:0)

<body>

    <button onclick="window.location.href=getNextPage(document.URL)">Submit</button>

</body>
<script>
    function getNextPage(title){
        debugger;
        var currentPage = parseInt(title.slice(-6,-5));
        console.log(currentPage);
        var nextPage;
        if(currentPage==10){
            var nextPage = 0
        }
        else{
            var nextPage = ++currentPage;
        }
        return "page"+nextPage+".html";
    }
</script>

此方法解析当前页面的URL,然后使用它来增加指示下一页的内容的数字。我对此进行了简单的测试,但是如果有任何我可以帮助的话,请告诉我。

注意:当前页面切片仅在页面URL的格式为page1.html时才有效,依此类推。如果对该格式进行任何更改,请相应地调整此行。

答案 2 :(得分:0)

您可以使用Window.localStorage

执行此操作

<强> 实施例

function goTonextPage() {
  if (!localStorage.lastPage) {
      localStorage.lastPage = 0;
  }
  var nextPage = Number(localStorage.lastPage) === 10 ? 1 : Number(localStorage.lastPage) + 1;
  localStorage.lastPage = nextPage;
  return window.location.href = '/page' + nextPage + '.html';
}
<button onclick="goToNextPage();">Submit</button>

答案 3 :(得分:0)

创建文件 paginator.js

添加:

_map(people, function(obj) {
  return _.assign(obj, _.find(secondArray, {id: obj.id}));
})

您的按钮应为:

function navigateToNext() {
     var regex = /page([\d]+).html/;
     var matches = regex.exec(location.pathname);
     if (matches[1]) {
         localtion.href = "/page"+((matches[1]+1)%10+1)+".html";
     }
}

不要忘记将脚本包含在所需的所有页面中:

<button onclick="navigateToNext()">Submit</button>
相关问题