!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)
//當(dāng)前所在的屏數(shù),如curPage=21,總頁數(shù)為50頁時,屏數(shù)就是3(當(dāng)前是第幾屏)實(shí)際上是這種形式:Math.ceil(curPage*pageSize/pageSize*perPage)
var totalBigPage = Math.ceil(totalPages/middlePage); //總的屏數(shù)
if( totalPages middlePage){
for(var i=1;itotalPages+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('現(xiàn)在取第 '+i+ ' 頁數(shù)據(jù)'); //ajax get data
}
initPage(50,2,10,'demo');
// -->/script>
/body>
/html>
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》