Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从1数到10,在按钮链接内重复_Javascript_Php_Jquery_Html - Fatal编程技术网

Javascript 从1数到10,在按钮链接内重复

Javascript 从1数到10,在按钮链接内重复,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我正在寻找一种方法来编写一个从1到10计数的脚本,10之后它将在buttom链接中重复 例如: <button onclick="window.location.href='/page"1".html'">Submit</button> 提交 第一次点击进入“url/page1.html” 第二次点击“url/page2.html” “url/page3.html”的第三个 “url/page4.html”的第四个 最多十次点击“url/page10.html” 在命

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

例如:

<button onclick="window.location.href='/page"1".html'">Submit</button>
提交
第一次点击进入“url/page1.html”

第二次点击“url/page2.html”

“url/page3.html”的第三个

“url/page4.html”的第四个

最多十次点击“url/page10.html”

在命中数字10后,它必须再次重复运行到1

例2:

首先单击转到“url/page1.html”,单击2=“url/page2.html”,单击3=“url/page3.html”,单击4=“url/page4.html,单击5=“url/page5.html,单击5=“url/page5.html,单击6=“url/page6.html,单击7=“url/page7.html,单击8=“url/page8.html,单击9=“url/page9.html”单击10=“url/page10.html,然后重复单击11=“url/page1.html,单击12=“url/page2.html,单击13=“url/page3.html,单击14=“url/page4.html


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

这个解决方案怎么样:

<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>
提交
设currentPage=1;
函数myScript(totalPages){
如果(当前页面===总页面){
currentPage=1;
返回window.location.href=`/page10.html`
}
currentPage=currentPage
我没有对此进行测试,但我相信,即使可能会有错误,全局推理也会对您有效。我将总页数作为函数的参数,这一事实允许您在其他情况下再次使用此函数

你能告诉我是不是吗?


<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>
提交 函数getNextPage(标题){ 调试器; var currentPage=parseInt(title.slice(-6,-5)); console.log(当前页面); var下一页; 如果(当前页面==10){ var nextPage=0 } 否则{ var nextPage=++currentPage; } 返回“page”+nextPage+“.html”; }
此方法解析当前页面的URL,然后使用它增加指示下一页内容的数字。我已经对此进行了测试,但如果有什么可以帮助的,请告诉我

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

您可以使用

示例:

函数goTonextPage(){
如果(!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';
}

Submit
创建文件paginator.js

加:

您的按钮应为:

<button onclick="navigateToNext()">Submit</button>
提交
不要忘记在需要的所有页面中包含脚本:

<script src='paginator.js'></script>


一个从1到10计数的脚本,在10之后它会重复,它会重复什么?在10到1之后。你想让它无限循环吗?我有点猜测,但你想找出某种类型的分页吗?首先单击转到“url/page1.html”,单击2=“url/page2.html”,单击3=“url/page3.html”,单击4=“url/page4.html,点击5=“url/page5.html,点击5=“url/page5.html,点击6=“url/page6.html,点击7=“url/page7.html,点击8=“url/page8.html,点击9=“url/page9.html,点击10=“url/page10.html”,然后重复点击11=“url/page1.html,点击12=“url/page2.html,点击13=“url/page3.html,点击14=“url/page4.html希望您能理解我的意思。除了每次加载新页面(从
window.location.href
)时,当前页面计数器将被重置…它从1工作到10,但在10之后运行10不重复您知道,第一次单击后,页面将更改为另一个页面,因此所有脚本都将从头开始,这意味着
currentPage
将永远等于
1
?哦,你完全正确,我不敢相信我错过了这个@M.jacobsem您只需要找到一种方法使
currentPage
成为一个一致的变量(例如,您可以使用
localStorage
来实现这一点):)让我检查一下我做错了什么,然后想出一个更好的解决方案:)您可以使用RegExp从URL中提取页码,类似于
/^.*page(\d{1,2})\.html$/
以避免切片问题。
<script src='paginator.js'></script>