我有一组网址,例如
pageList = ["index.html", "01.html", "02.html"]
并且我想创建一个函数,其中.prev和.next转到该数组中的上一个/下一个项目,因此如果我在index.html中,则单击“下一步”将转到“ 01.html”,如果我m在“ 01.html”中将转到“ 02.html”,依此类推。
我该怎么办?我发现了许多“分页” jquery插件,但它们似乎都可以通过同一URL中的div分页,但是我需要一些实际上可以转到另一个页面的东西,例如
window.location = currentLocation + 1
很抱歉菜鸟问题,它确实来自菜鸟。!
您可以这样操作:
var pageList = ["index.html", "01.html", "02.html"];
var url = window.location.pathname; // e.g. http://me.com/index.html
var page = url.substring(url.lastIndexOf('/')+1); // e.g. index.html
var currentPosition = pageList.indexOf(page); // e.g. 0, 1 or 2
document.getElementById('next-btn').onclick = function(){
// if the URL is not in the list, do nothing
if(currentPosition==-1) return;
// if we're at the end, go back to index, else, go to next page
window.location = currentPosition<pageList.length-1? pageList[++currentPosition]:pageList[0];
}
在每个页面上插入该脚本,如果next-btn
要单击ID的元素,它应该可以工作。
如果您不熟悉最后一行的语法,也可以这样写:
if(currentPosition<pageList.length-1) {window.location = pageList[++currentPosition];}
else {window.location = pageList[0];}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句