如何创建上一个/下一个按钮算法



我正在建立一个以下页面的重复名称的网站:

 thesame/thesame/1-1.html    
 thesame/thesame/1-2.html
 thesame/thesame/1-3.html

第一个号码是章节号,第二个是页码,我需要插入一个previous/next按钮,下一个按钮将1添加到当前HREF和重定向(如果您在第1-1-1.html中您到第1-2.html,然后是1-3.html),然后从当前HREF中减去1。我尝试了以下内容,但会影响第一个数字(numper notem not Page编号):

 var p = (window.location.href.split('/')[window.location.href.split(‌​'/').length-1].split‌​('.')[0].split('-')[‌​0]*1)+1;

这是一个演示

<!DOCTYPE html>
<html>
<body>
<p>Click the button to locate where in the string a specifed value  occurs.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "http://192.168.1.3:33334/Preview/the-website/3-37.html";
var html = (str.split('/')[str.split('/').length-1].split('-')[0]*1)+1;
document.getElementById("demo").innerHTML = html;
}
</script>
</body>
</html>

有什么想法?

最好这样做:

<html>
<head>
<script>
function next(c){
    var maxp = 5; 
    var p = (window.location.href.split('/')[window.location.href.split('/').length-1].split('.')[0].split('-')[1]*1)+1; 
    window.location.href = (p > 0 && p <= maxp) ? c + '-' + p + '.html' : '#';
}
function prev(c){
    var maxp = 5; 
    var p = (window.location.href.split('/')[window.location.href.split('/').length-1].split('.')[0].split('-')[1]*1)-1; 
    window.location.href = (p > 0 && p <= maxp) ? c + '-' +p + '.html' : '#';
}
</script>
</head>
<body>
<a onclick="next(1)">next</a>
<a onclick="prev(1)">prev</a>
</body>
</html>

将章节编号作为下一步的参数和prev函数的参数

传递

如果您不喜欢使用功能调用,请使用它:对于下一个按钮:

<a onclick="javascript: var maxp = 5; var p = (window.location.href.split('/')[window.location.href.split('/').length-1].split('.')[0]*1)+1; window.location.href = (p > 0 && p <= maxp) ? '1-'+ p + '.html' : '#'">next</a>

对于上一个按钮:

<a onclick="javascript: var maxp = 5; var p = (window.location.href.split('/')[window.location.href.split('/').length-1].split('.')[0]*1)-1; window.location.href = (p > 0 && p <= maxp) ? '1-' + p + '.html' : '#'">prev</a>

在这里,maxp是最后一页索引

最新更新