2. [代码][JavaScript]代码
/*!
* jQuery initPage
* Author:zdyue
*/
$.extend({
//obj:分页对象; currentNum:当前页; totalNum:总页数; callBackEvent:回调函数
initPage : function(options){
if(!options.obj){ return false; };
var obj = $(options.obj);
var currentNum = options.currentNum || 1;
var totalNum = options.totalNum || 5;
var callBackEvent = options.callBackEvent || function(){};
var strpage = "";
function ele(num, cls){
var str = "";
if(cls){
str = "
"+num+"";
}else{
str = "
"+num+"";
}
return str;
}
//Current page is greater than 1 shows previous page
if (currentNum > 0) {
strpage = "
首页";
obj.append(strpage);
}
if (currentNum > 1) {
strpage = "
<<";
obj.append(strpage);
}
if(totalNum <= 5){
for(var i=1; i<=totalNum; i++){
if(currentNum == i){
strpage = ele(i, "now");
}else{
strpage = ele(i);
}
obj.append(strpage);
}
}else{
for(var i=1; i<=5; i++){
if(currentNum == 1 || currentNum == 2){
if(currentNum == i){
strpage = ele(i, "now");
}else{
strpage = ele(i);
}
}else if( (totalNum - currentNum) == 0 || (totalNum - currentNum) == 1 ){
if( (totalNum - currentNum) == 0 && i == 5){
strpage = ele( (totalNum - 5 + i), "now");
}else if( (totalNum - currentNum) == 1 && i == 4){
strpage = ele( (totalNum - 5 + i), "now");
}else{
strpage = ele( totalNum - 5 + i );
}
}else{
if(i == 3){
strpage = ele(currentNum-3+i, "now");
}else{
strpage = ele(currentNum-3+i);
}
}
obj.append(strpage);
}
}
if ((totalNum - currentNum) >= 2) {
strpage = "
...";
obj.append(strpage);
}
if ((totalNum - currentNum) >= 1) {
strpage = "
>>";
obj.append(strpage);
strpage = "
末页";
obj.append(strpage);
}
callBackEvent(currentNum, totalNum);
obj.find("a").click(function(){
var currentNum = parseInt($(this).attr("href").substring(1));
obj.html("");
$.initPage({
obj : "#page",
currentNum : currentNum,
totalNum : totalNum,
callBackEvent :callBackEvent
});
return false;
});
}
})
3. [代码]调用
分页