提问人:Nakul Bhat 提问时间:11/10/2023 更新时间:11/10/2023 访问量:31
如何使用变量重定向将下一个和上一个按钮添加到已构建的页面 [已关闭]
How to add next and prev buttons to already built pages with variable redirects [closed]
问:
因此,我建立了这个 jouraling 网站作为自己的消遣时间。我将所有条目作为单独的网页放在一个文件夹中。我注意到的一件事是,我没有办法继续阅读网站。
我想添加将根据当前 url 重定向的按钮。例如,在第 5.html 页上,下一个按钮将重定向到 6.html,上一个按钮将重定向到 4.html。
我的所有条目都遵循上述命名方案,因此这不是问题。
我能想到的唯一解决方案是手动添加带有正确链接的按钮。有 35 页(更多)页面,所以我不急于暴力破解它
我绝对认为有更好的方法可以做到这一点,所以我问了一些比我聪明的人。
答:
0赞
Nakul Bhat
11/10/2023
#1
下面的JS代码
let str = window.location.href;
console.log("Original String: " + str + "<br/>");
// Matching for the numbers using the match() method.
let numbers = str.match(/\d+/g);
console.log("Numbers: " + numbers + "<br/>");
// Converting the numbers into integer using parseInt() method.
numbers = parseInt(numbers, 10);
let nexturl = "file:///home/nakul/gitclones/thegoodjar/entries/" + (numbers+1) + ".html";
function next (){
location.assign(nexturl);
}
function prev (){
location.assign("file:///home/nakul/gitclones/thegoodjar/entries/" + (numbers-1) + ".html");
}
还有很大的改进空间,但主要我应该努力使这段代码具有可移植性。
评论
0赞
ADyson
11/10/2023
不过,您仍然必须将其粘贴到所有文件中,除非它们共享一个通用的头文件?
评论