1. 程式人生 > >js分頁代碼分享

js分頁代碼分享

tom false ide ror check con wid read utl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script  type=‘text/javascript‘ src=‘jQuery.js‘></script>
<style type="text/css">
span{width: 60px;height: 20px;display: inline-block;border: solid 1px black;text-align: center;margin: 5px;cursor:pointer;}
</style>
</head>
<body>
<div id=‘page‘></div>
<div id=‘con‘></div>
<script type=‘text/javascript‘>
 var arr=new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
 /*
 顯示那一頁的內容
 arr內容數組,那頁的從哪條開始顯示的,len就是每頁顯示內容條數
 */
 function pageCon(arr,start,len){
  var constr=‘‘;
  for(var i=start;i<start+len;i++){
   if(arr[i-1]){
         constr+=arr[i-1]+"<br/>";
     }
  }
  return constr;
 }
function show(arr,now_page){
  var sum=arr.length;
  var page_size=3;
  var page_num=Math.ceil(sum/page_size);
  var start=(now_page-1)*page_size+1;
  var page_str=‘‘;
    for(var i=1;i<=page_num;i++){
     var stylei=i;
     if(i==now_page){  stylei="<span style=‘background:black;color:white;margin:0‘>"+i+"</span>";}
        page_str+=‘<span onclick="show(arr,‘+i+‘)">‘+stylei+‘</span>‘;
    }
  $(‘#page‘).html(page_str);
  var constr=pageCon(arr,start,page_size);
  $(‘#con‘).html(constr);
}
show(arr,2);
</script>
</body>
</html>
47
  var page_size=3;
1
<html>
2
<head>
3
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4
<title>Insert title here</title>
5
<script  type=‘text/javascript‘ src=‘jQuery.js‘></script>
6
<style type="text/css">
7
span{width: 60px;height: 20px;display: inline-block;border: solid 1px black;text-align: center;margin: 5px;cursor:pointer;}
8
</style>
9
</head>
10
<body>
11
<div id=‘page‘></div>
12
<div id=‘con‘></div>
13
<script type=‘text/javascript‘>
14
 var arr=new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
15
 /*
16
 顯示那一頁的內容
17
 arr內容數組,那頁的從哪條開始顯示的,len就是每頁顯示內容條數
18
 */
19
 function pageCon(arr,start,len){
20
  var constr=‘‘;
21
  for(var i=start;i<start+len;i++){
22
   if(arr[i-1]){
23
         constr+=arr[i-1]+"<br/>";
24
     }
25
  }
26
  return constr;
27
 }
28
function show(arr,now_page){
29
  var sum=arr.length;
30
  var page_size=3;
31
  var page_num=Math.ceil(sum/page_size);
32
  var start=(now_page-1)*page_size+1;
33
  var page_str=‘‘;
34
    for(var i=1;i<=page_num;i++){
35
     var stylei=i;
36
     if(i==now_page){  stylei="<span style=‘background:black;color:white;margin:0‘>"+i+"</span>";}
37
        page_str+=‘<span onclick="show(arr,‘+i+‘)">‘+stylei+‘</span>‘;
38
    }
39
  $(‘#page‘).html(page_str);
40
  var constr=pageCon(arr,start,page_size);
41
  $(‘#con‘).html(constr);
42
}
43
show(arr,2);
44
</script>
45
</body>
46
</html>

js分頁代碼分享