我有根据可用页码和您当前所在的 URL 设置 Prev 和 Next URL 的工作代码。
我的代码太长了,一点也不灵活。我怎样才能使它更灵活并为生产做好准备?
http://jsfiddle.net/sunflowersh/ppDL7/5/
var currentindex = '4'; //updated based on the current index data
var indexNum = parseInt(currentindex);
var currentURL = document.location.href.substring(document.location.href.lastIndexOf("/")+1, document.location.href.length);
var baseURL ="/en_US/";
var leftURL = "";
var rightURL = "";
switch(indexNum)
{
case 2:
// if there are only index, index1, and index2
if((currentURL.indexOf("index.html")>-1)||(currentURL.indexOf("index.")>-1)){
leftURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
} else {
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
}
break;
case 3:
// if on index3.html or index.html
// make the right arrow URL go back to index1.html
if((currentURL.indexOf("index3.html")>-1)||(currentURL.indexOf("index.")>-1)){
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum-2) + ".html";
} else {
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
}
break;
case 4:
if((currentURL.indexOf("index4.html")>-1)||(currentURL.indexOf("index.")>-1)){
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum-3) + ".html";
} else {
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
}
break;
case 5:
if((currentURL.indexOf("index5.html")>-1)||(currentURL.indexOf("index.")>-1)){
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum-3) + ".html";
} else {
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
}
break;
default:
// if no current index, disable the link
leftURL= baseURL + "#";
rightURL= baseURL + "#";
}
var leftArrow =$(".leftArrow").find("a").attr("href",leftURL);
var rightArrow =$(".rightArrow").find("a").attr("href",rightURL);