在 javascript中将数组进行分页处理
在 javascript中将数组进行分页处理
2007-04-26 15:03
一个数组的结果如下array.js:
var array=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]; 现在我们要将这个数组进行分页处理,此方法可以用于在客户端进行分页处理,比如对于远程获取的xml数据之类的我相信大家应该看过迅雷资源博客上的分页处理吧,此方法和它比较类似。 <!-- power by:antsnet.net email:antsnet@163.com --> <html> <head> <title>array page</title> <script language="javascript" src="array.js"></script> <script language="javascript" src="../js/ajax.js"></script> <script language="javascript"> /*global define*/ var pagesize=5; var length=array.length; var total=length/pagesize; function page(pagesize,page){ var string=""; var start=(Number(page)-1)*pagesize; if(start<0){ start=0; } if(page<1){ page=1; }else if(page>total){ page=total; } var tend=Number(page)*pagesize; var prev=Number(page)-1; if(page==total){ var next=page; }else{ var next=Number(page)+1; } var end=Number(total); for(var i=start;i<tend;i++){ string+=array[i]; } $("array").innerHTML=string; $("page").innerHTML="<a href=\"javascript:page('"+pagesize+"',0);\">one</a> <a href=\"javascript:page('"+pagesize+"','"+prev+"');\">prev</a> <a href=\"javascript:page('"+pagesize+"','"+next+"');\">next</a> <a href=\"javascript:page('"+pagesize+"','"+end+"');\">end</a>"; } </script> </head> <body> <div id="array">loading^^^</div> <div id="page">loading^^^</div> </body> </html> <script language="javascript"> page(5,1); |