js分页 和 可输入下拉框 代码

function page(opt) {

if (!opt.id) { return false };

var obj = document.getElementById(opt.id);

var nowNum = opt.nowNum || 1;
var allNum = opt.allNum || 5;

var callBack = opt.callBack || function () { };
/*................................显示首页...................................................*/
if (nowNum >= 4 && allNum >= 6) {

var oA = document.createElement(‘a‘);
oA.href = ‘#1‘;
oA.innerHTML = ‘首页‘;
obj.appendChild(oA);

}
/*................................显示上一页...................................................*/
if (nowNum >= 2) {
var oA = document.createElement(‘a‘);
oA.href = ‘#‘ + (nowNum - 1);
oA.innerHTML = ‘上一页‘;
obj.appendChild(oA);
}
/*..................................总共页数小于5个..............................................*/
if (allNum <= 5) {
for (var i = 1; i <= allNum; i++) {
var oA = document.createElement(‘a‘);
oA.href = ‘#‘ + i;
if (nowNum == i) {
oA.innerHTML = i;
}
else {
oA.innerHTML = ‘[‘ + i + ‘]‘;
}
obj.appendChild(oA);
}
} else {
/*..................................总共页数大于5个..............................................*/
for (var i = 1; i <= 5; i++) {//中间五项
var oA = document.createElement(‘a‘);

/*...................中间前两项.....................................*/
if (nowNum == 1 || nowNum == 2) {

oA.href = ‘#‘ + i;
if (nowNum == i) {
oA.innerHTML = i;
}
else {
oA.innerHTML = ‘[‘ + i + ‘]‘;
}

}
/*...................中间最后两项.....................................*/
else if ((allNum - nowNum) == 0 || (allNum - nowNum) == 1) {

oA.href = ‘#‘ + (allNum - 5 + i);

if ((allNum - nowNum) == 0 && i == 5) {//最后一个
oA.innerHTML = (allNum - 5 + i);
}
else if ((allNum - nowNum) == 1 && i == 4) {//倒数第二个
oA.innerHTML = (allNum - 5 + i);
}
else {
oA.innerHTML = ‘[‘ + (allNum - 5 + i) + ‘]‘;
}

}
else
/*...................中间一项.....................................*/ {
oA.href = ‘#‘ + (nowNum - 3 + i);

if (i == 3) {
oA.innerHTML = (nowNum - 3 + i);
}
else {
oA.innerHTML = ‘[‘ + (nowNum - 3 + i) + ‘]‘;
}
}
obj.appendChild(oA);

}

}
/*......................................................................................................*/
if ((allNum - nowNum) >= 1) {
var oA = document.createElement(‘a‘);
oA.href = ‘#‘ + (nowNum + 1);
oA.innerHTML = ‘下一页‘;
obj.appendChild(oA);
}

if ((allNum - nowNum) >= 3 && allNum >= 6) {

var oA = document.createElement(‘a‘);
oA.href = ‘#‘ + allNum;
oA.innerHTML = ‘尾页‘;
obj.appendChild(oA);

}
/*..................................加点击事件.................................................*/
callBack(nowNum, allNum);

var aA = obj.getElementsByTagName(‘a‘);

for (var i = 0; i < aA.length; i++) {
aA[i].onclick = function () {

var nowNum = parseInt(this.getAttribute(‘href‘).substring(1));

obj.innerHTML = ‘‘;

page({

id: opt.id,
nowNum: nowNum,
allNum: allNum,
callBack: callBack

});

return false;

};
}

}

//在使用之前先获得总数

page({
id: ‘page‘,
nowNum: page_index,   //当前页
allNum: Math.ceil(total * 1.00 / 10 * 1.00),  //总页数
callBack: function (now, all)  //回调函数   now是当前页
{
page_index = now;
loaddata();
}

});

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。