DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX基礎知識 >> js實現ajax分頁完整實例
js實現ajax分頁完整實例
編輯:AJAX基礎知識     

本文實例講述了js實現ajax分頁的方法。分享給大家供大家參考,具體如下:

<!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>Untitled Document</title>
</head>
<body>
<style type="text/css">
*{margin:0;padding:0}
.ajax_page{padding:1px 4px;border:1px solid #e60011;margin:0 2px;text-decoration:none;color:#666666;font-family:mingliu;font-size:11px;height:14px;line-height:14px;float:left;font-weight:bold;display:block;}
span.currentPage{padding:2px 4px;color:#666666;font-size:11px;height:14px;line-height:14px;float:left;display:block;font-weight:bold;font-family:mingliu}
</style>
<style type="text/css">
*{margin:0;padding:0}
.ajax_page{padding:1px 4px;border:1px solid #e60011;margin:0 2px;text-decoration:none;color:#666666;font-family:mingliu;font-size:11px;height:14px;line-height:14px;float:left;font-weight:bold;display:block;}
span.currentPage{padding:2px 4px;color:#666666;font-size:11px;height:14px;line-height:14px;float:left;display:block;font-weight:bold;font-family:mingliu}
</style>
<div id="demo" style="width:500px;margin:10px auto;"></div>
<script type="text/JavaScript">
<!--
function initPage(totalPages,curPage,middlePage,Container){
  var htmlstr = '';
  if(curPage > totalPages) {curPage = totalPages}
  if(curPage < 0) {curPage = 1}
  var curBigPage = Math.ceil(curPage/middlePage)
  //當前所在的屏數,如curPage=21,總頁數為50頁時,屏數就是3(當前是第幾屏)實際上是這種形式:Math.ceil(curPage*pageSize/pageSize*perPage)
  var totalBigPage = Math.ceil(totalPages/middlePage); //總的屏數
  if( totalPages < middlePage){
    for(var i=1;i<totalPages+1;i++){
      if(i == curPage){
        htmlstr += '<span class="currentPage">' + i + '</span>';
      }else{
        htmlstr += '<a href="javascript:void(0)" mce_href="javascript:void(0)" onclick="initPage('+ totalPages+','+i+','+middlePage +',/''+ container+'/');getPageData('+ i+');return false" class="ajax_page">'+i +'</a>';
      }
    }
  }else{
    var curBigStart = (curBigPage-1)*middlePage + 1;
    if(curBigPage == totalBigPage){
      var curBigEnd = totalPages;
    }else{
      var curBigEnd = curBigPage * middlePage;
    }
    if(curBigPage != 1){  //前一屏
      var preCurPage = curBigStart - middlePage;
      htmlstr += '<a href="javascript:void(0)" mce_href="javascript:void(0)" onclick="initPage('+ totalPages+','+preCurPage+','+middlePage +',/''+ container+'/');getPageData('+ preCurPage +');return false" class="ajax_page">pre</a>';
    }
    for(var i=curBigStart;i<=curBigEnd;i++){
      if(i == curPage){
        htmlstr += '<span class="currentPage">' + i + '</span>';
      }else{
        htmlstr += '<a href="javascript:void(0)" mce_href="javascript:void(0)" onclick="initPage('+ totalPages+','+i+','+middlePage +',/''+ container+'/');getPageData('+ i +');return false" class="ajax_page">' + i + '</a>';
      }
    }
    if(curBigPage != totalBigPage){  //後一屏
      var nextCurPage = curBigStart + middlePage;
      htmlstr += '<a href="javascript:void(0)" mce_href="javascript:void(0)" onclick="initPage('+ totalPages+','+nextCurPage+','+middlePage +',/''+ container+'/');getPageData('+ nextCurPage + ');return false" class="ajax_page">next</a>';
    }
  }
  document.getElementById('demo').innerHTML = htmlstr
}
function getPageData(i){
  alert('現在取第 '+i+ ' 頁數據');  //ajax get data
}
initPage(50,2,10,'demo');
// --></script>
</body>
</html>

更多關於JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結》、《JavaScript查找算法技巧總結》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結》

希望本文所述對大家JavaScript程序設計有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved