+ -
当前位置:首页 → 问答吧 → JS下一页代码,遇到2位数以上的9读取链接错误

JS下一页代码,遇到2位数以上的9读取链接错误

时间:2010-06-02

来源:互联网

实现功能:序列的页码,链接到下一页。比如:001.htm,002.htm,003.htm......

遇到问题:009.htm的时候可以正常链接下一页是010.htm,但是到019的时候就出错了,望前辈们帮助解决或是有更好的实现代码希望不吝赐教,谢谢~!

源码如下:(另存为019.htm测试效果)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=" http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>019.htm</title> <script type="text/javascript"> function page(){ var temp = window.location.href; var reMatch = /^(.*[^0])(0*)(\d+)\.(\w+)$/; if(reMatch.test(temp)){ newHref = parseInt(RegExp.$3) + 1; newHref = ((newHref%10)!=0)?(RegExp.$1 + RegExp.$2 + newHref + "." + RegExp.$4):(RegExp.$1 + RegExp.$2.substring(0,RegExp.$2.length-1) + newHref + "." + RegExp.$4); var aNode = document.getElementById("pager"); aNode.setAttribute("href",newHref); } } window.onload = page; </script> </head> <body> <p><a id="pager" href="#">下一页</a></p> </body> </html>
 提示:您可以先修改部分代码再运行

作者: 1key   发布时间: 2010-06-02


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=" http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>019.htm</title> <script type="text/javascript"> function page(){ //var temp = window.location.href; var temp = 'http://test.com/039.htm'; var reMatch = /^.+\/(\d+)\.(\w+)$/, newHref; if(reMatch.test(temp)){ newHref = '000' + (parseInt(RegExp.$1, 10) + 1);//当前页码加1, 左侧补上三个0 newHref = newHref.substr(newHref.length - 3);//右截3位 var aNode = document.getElementById("pager"); aNode.setAttribute("href", newHref + '.' + RegExp.$2);//设置下一页的地址 } } window.onload = page; </script> </head> <body> <p><a id="pager" href="#">下一页</a></p> </body> </html>
 提示:您可以先修改部分代码再运行

作者: faeng220   发布时间: 2010-06-03

  感谢版主的解答,不过通用性好像没有原来的强,原来文件名如abc001,abc002。。。或1-25,1-26。。。都可以链接下一页,只是遇到9的时候就会出错。貌似版主修改的版本只能局限在纯数字序列的文件名上。我不懂程序,如果问题不符合逻辑还请版主不要见笑 =.=
  不过已经够用了,我把文件名规定死为001,002这样的序列就好了。非常非常感谢版主,多谢~

作者: 1key   发布时间: 2010-06-04


你举的例子中没有说文件名开头有别的东西吧,
文件名格式为:
字母数字.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=" http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>019.htm</title> <script type="text/javascript"> function page(){ //var temp = window.location.href; var temp = 'http://test.com/hello039.htm'; var reMatch = /^.+\/([^\d]*)(\d+)\.(\w+)$/, newHref; if(reMatch.test(temp)){ newHref = '000' + (parseInt(RegExp.$2, 10) + 1);//当前页码加1, 左侧补上三个0 newHref = newHref.substr(newHref.length - 3);//右截3位 var aNode = document.getElementById("pager"); aNode.setAttribute("href", RegExp.$1 + newHref + '.' + RegExp.$3);//设置下一页的地址 } } window.onload = page; </script> </head> <body> <p><a id="pager" href="#">下一页</a></p> </body> </html>
 提示:您可以先修改部分代码再运行
文件名格式为的例子(代码与上面的一样,只是URL地址不样):
纯数字.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=" http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>019.htm</title> <script type="text/javascript"> function page(){ //var temp = window.location.href; var temp = 'http://test.com/039.htm'; var reMatch = /^.+\/([^\d]*)(\d+)\.(\w+)$/, newHref; if(reMatch.test(temp)){ newHref = '000' + (parseInt(RegExp.$2, 10) + 1);//当前页码加1, 左侧补上三个0 newHref = newHref.substr(newHref.length - 3);//右截3位 var aNode = document.getElementById("pager"); aNode.setAttribute("href", RegExp.$1 + newHref + '.' + RegExp.$3);//设置下一页的地址 } } window.onload = page; </script> </head> <body> <p><a id="pager" href="#">下一页</a></p> </body> </html>
 提示:您可以先修改部分代码再运行

作者: faeng220   发布时间: 2010-06-04

相关阅读 更多