js實現ajax分頁完整執行個體_AJAX相關

來源:互聯網
上載者:User

本文執行個體講述了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程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.