jQuery實(shí)現(xiàn)的分頁(yè)插件完整示例
本文實(shí)例講述了jQuery實(shí)現(xiàn)的分頁(yè)插件。分享給大家供大家參考,具體如下:
呈現(xiàn)

html文件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="引入一個(gè)jquery文件,這里就不提供了"></script>
<link rel="stylesheet" href="引入下邊提供的css文件" rel="external nofollow" >
</head>
<body>
<div id="pages" class="devidePage" ></div>
</body>
<script>
var pages=10; //計(jì)算出總頁(yè)數(shù)(一定要是5的倍數(shù))
function getData(num){
/*當(dāng)前頁(yè)數(shù)*/
var currentPageNum = num;
/*取數(shù)據(jù)*/
$.ajax({
type: "post",
url: url, /*請(qǐng)求的servlet的地址*/
data: {"currentPageNum":currentPageNum},
cache: false,
async : false,
dataType: "json",
success: function (data ,textStatus, jqXHR)
{
if("true"==data.flag){
setData(data.data);
}else{
console.log("不合法!錯(cuò)誤信息如下:"+data.errorMsg);
}
},
error:function (XMLHttpRequest, textStatus, errorThrown) {
console.log("請(qǐng)求失敗!");
}
});
}
function setData(data){
/*放數(shù)據(jù)*/
}
</script>
<script src="引入下邊提供的js文件"></script>
</html>
css文件
@charset "UTF-8";
/*分頁(yè)所在的div*/
.devidePage{
margin-top:300px;
margin-left: 400px;
height: 50px;
width: 800px;
/* background: gray; */
}
/*顯示頁(yè)數(shù)的div*/
.pages{
float:left;
margin-left:2px;
height:50px;
width:50px;
background: #EEEEEE;
text-align:center;
line-height:50px;
cursor:pointer;
}
/*首頁(yè)*/
.theFirstPage{
float:left;
margin-left:2px;
height:50px;
width:50px;
background: #EEEEEE;
text-align:center;
line-height:50px;
cursor:pointer;
}
/*末頁(yè)*/
.theLastPage{
float:left;
margin-left:2px;
height:50px;
width:50px;
background: #EEEEEE;
text-align:center;
line-height:50px;
cursor:pointer;
}
/*上一頁(yè)*/
.prePage{
float:left;
margin-left:2px;
height:50px;
width:50px;
background: #EEEEEE;
text-align:center;
line-height:50px;
cursor:pointer;
}
/*下一頁(yè)*/
.nextPage{
float:left;
margin-left:2px;
height:50px;
width:50px;
background: #EEEEEE;
text-align:center;
line-height:50px;
cursor:pointer;
}
/*當(dāng)前頁(yè)數(shù)*/
.currentPage{
float:left;
margin-left:2px;
height:50px;
width:100px;
background: #EEEEEE;
text-align:center;
line-height:50px;
}
/*總頁(yè)數(shù)*/
.pageNums{
float:left;
margin-left:2px;
height:50px;
width:100px;
background: #EEEEEE;
text-align:center;
line-height:50px;
}
/*輸入頁(yè)數(shù)*/
.jump{
float:left;
margin-left:2px;
height:48px;
width:50px;
border:0.5px solid #EEEEEE;
}
/*跳轉(zhuǎn)*/
.jumpClick{
float:left;
margin-left:2px;
height:50px;
width:50px;
background: #EEEEEE;
text-align:center;
line-height:50px;
cursor:pointer;
}
js文件
/**
* 俠 2018-8-15
*/
function loadAll() {
var theFirstPage = "<div class=\"theFirstPage\" οnclick=\"theFirstPage()\">首頁(yè)</div>";
var prePage = "<div class=\"prePage\" οnclick=\"prePage()\">上一頁(yè)</div>";
var pagess = "<div id=\"page_1\" class=\"pages\" οnclick=\"changePage(this.id)\">1</div>"
+ "<div id=\"page_2\" class=\"pages\" οnclick=\"changePage(this.id)\">2</div>"
+ "<div id=\"page_3\" class=\"pages\" οnclick=\"changePage(this.id)\">3</div>"
+ "<div id=\"page_4\" class=\"pages\" οnclick=\"changePage(this.id)\">4</div>"
+ "<div id=\"page_5\" class=\"pages\" οnclick=\"changePage(this.id)\">5</div>";
var nextPage = "<div class=\"nextPage\" οnclick=\"nextPage()\">下一頁(yè)</div>";
var theLastPage = "<div class=\"theLastPage\" οnclick=\"theLastPage()\">末頁(yè)</div>";
var currentPages = "<div id=\"currentPage\" class=\"currentPage\">第1頁(yè)</div>";
var pageNums = "<div id=\"pageNums\" class=\"pageNums\">共" + pages
+ "頁(yè)</div>";
var jump = "<input id=\"jump\" type=\"text\" class=\"jump\" "
+"οnkeyup=\"(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)\""
+" οnblur=\"this.v();\">";
var jumpClick = "<div class=\"jumpClick\" οnclick=\"jump()\">跳轉(zhuǎn)</div>";
$("#pages").html(theFirstPage +
prePage + pagess + nextPage + theLastPage + currentPages + pageNums + jump
+ jumpClick);
}
loadAll();
function defultBackground() {
$("#page_1").css("background", "#66b2ff"); //配置選中顏色
}
defultBackground();
function changeBackground() {
$(".pages").css("background", "#EEEEEE"); //配置默認(rèn)顏色
for (var i = 0; i < 5; i++) {
if ($("#page_" + (i + 1)).text() == $("#currentPage").text().split("第")[1]
.split("頁(yè)")[0]) {
$("#page_" + (i + 1)).css("background", "#66b2ff"); //配置選中顏色
break;
}
}
}
function theFirstPage(){
$('#currentPage').html("第" + 1 + "頁(yè)");
$("#page_1").html(1);
$("#page_2").html(2);
$("#page_3").html(3);
$("#page_4").html(4);
$("#page_5").html(5);
changeBackground();
getData(getCurrentPageNum());
}
function theLastPage(){
$('#currentPage').html("第" + pages + "頁(yè)");
$("#page_1").html(pages-4);
$("#page_2").html(pages-3);
$("#page_3").html(pages-2);
$("#page_4").html(pages-1);
$("#page_5").html(pages);
changeBackground();
getData(getCurrentPageNum());
}
function changePage(id) {
var pagenum = parseInt($("#" + id).text()) - 1;
$('#currentPage').html("第" + $("#" + id).text() + "頁(yè)");
if ((id.split("_")[1] == 1) && (parseInt($("#" + id).text()) > 1)) {
$("#page_1").html(parseInt($("#page_1").text()) - 1);
$("#page_2").html(parseInt($("#page_2").text()) - 1);
$("#page_3").html(parseInt($("#page_3").text()) - 1);
$("#page_4").html(parseInt($("#page_4").text()) - 1);
$("#page_5").html(parseInt($("#page_5").text()) - 1);
}
if ((id.split("_")[1] == 5) && (parseInt($("#" + id).text()) < pages)) {
$("#page_1").html(parseInt($("#page_1").text()) + 1);
$("#page_2").html(parseInt($("#page_2").text()) + 1);
$("#page_3").html(parseInt($("#page_3").text()) + 1);
$("#page_4").html(parseInt($("#page_4").text()) + 1);
$("#page_5").html(parseInt($("#page_5").text()) + 1);
}
changeBackground();
getData(getCurrentPageNum());
}
function prePage() {
var currentPageNumStr = $("#currentPage").text().split("第")[1].split("頁(yè)")[0];
var currentPageNum = parseInt(currentPageNumStr);
if (currentPageNum > 1) {
var toPageNum = currentPageNum - 1;
$("#currentPage").html("第" + toPageNum + "頁(yè)");
if ((currentPageNum > 1) && ($("#page_1").text() != 1)) {
$("#page_1").html(parseInt($("#page_1").text()) - 1);
$("#page_2").html(parseInt($("#page_2").text()) - 1);
$("#page_3").html(parseInt($("#page_3").text()) - 1);
$("#page_4").html(parseInt($("#page_4").text()) - 1);
$("#page_5").html(parseInt($("#page_5").text()) - 1);
}
changeBackground();
getData(getCurrentPageNum());
} else {
}
}
function nextPage() {
var currentPageNumStr = $("#currentPage").text().split("第")[1].split("頁(yè)")[0];
var currentPageNum = parseInt(currentPageNumStr);
if (currentPageNum < pages) {
var toPageNum = currentPageNum + 1;
$("#currentPage").html("第" + toPageNum + "頁(yè)");
if (currentPageNum >= 5 && ($("#page_5").text() != pages)) {
$("#page_1").html(parseInt($("#page_1").text()) + 1);
$("#page_2").html(parseInt($("#page_2").text()) + 1);
$("#page_3").html(parseInt($("#page_3").text()) + 1);
$("#page_4").html(parseInt($("#page_4").text()) + 1);
$("#page_5").html(parseInt($("#page_5").text()) + 1);
}
changeBackground();
getData(getCurrentPageNum());
} else {
}
}
function jump() {
var numstr = $("#jump").val();
var num = parseInt(numstr);
if ((num < 1) || (num > pages)) {
alert("輸入不合法");
$("#jump").val(1);
} else {
$("#currentPage").html("第" + num + "頁(yè)");
if (num >= 5) {
$("#page_5").html(num);
$("#page_4").html(num - 1);
$("#page_3").html(num - 2);
$("#page_2").html(num - 3);
$("#page_1").html(num - 4);
} else {
if (num = 4) {
$("#page_5").html(num + 1);
$("#page_4").html(num);
$("#page_3").html(num - 1);
$("#page_2").html(num - 2);
$("#page_1").html(num - 3);
}
if (num = 3) {
$("#page_5").html(num + 2);
$("#page_4").html(num + 1);
$("#page_3").html(num);
$("#page_2").html(num - 1);
$("#page_1").html(num - 2);
}
if (num = 2) {
$("#page_5").html(num + 3);
$("#page_4").html(num + 2);
$("#page_3").html(num + 1);
$("#page_2").html(num);
$("#page_1").html(num - 1);
}
if (num = 1) {
$("#page_5").html(num + 4);
$("#page_4").html(num + 3);
$("#page_3").html(num + 2);
$("#page_2").html(num + 1);
$("#page_1").html(num);
}
}
changeBackground();
getData(getCurrentPageNum());
}
}
function getCurrentPageNum(){
return parseInt( $("#currentPage").text().split("第")[1].split("頁(yè)")[0] );
}
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery切換特效與技巧總結(jié)》、《jQuery遍歷算法與技巧總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫(huà)與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- 深入研究jQuery圖片懶加載 lazyload.js使用方法
- jquery實(shí)現(xiàn)異步加載圖片(懶加載圖片一種方式)
- 詳解jQuery lazyload 懶加載
- jQuery+ajax實(shí)現(xiàn)滾動(dòng)到頁(yè)面底部自動(dòng)加載圖文列表效果(類似圖片懶加載)
- 基于jquery的圖片懶加載js
- jQuery插件實(shí)現(xiàn)圖片輪播效果
- JQuery插件tablesorter表格排序?qū)崿F(xiàn)過(guò)程解析
- jQuery彈框插件使用方法詳解
- viewer.js一個(gè)強(qiáng)大的基于jQuery的圖像查看插件(支持旋轉(zhuǎn)、縮放)
- jquery插件懶加載的示例
相關(guān)文章
利用jquery.qrcode在頁(yè)面上生成二維碼且支持中文
這篇文章主要介紹了利用jquery.qrcode在頁(yè)面上生成二維碼且支持中文。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
用jQuery.ajaxSetup實(shí)現(xiàn)對(duì)請(qǐng)求和響應(yīng)數(shù)據(jù)的過(guò)濾
本文主要對(duì)用jQuery.ajaxSetup實(shí)現(xiàn)對(duì)請(qǐng)求和響應(yīng)數(shù)據(jù)的過(guò)濾的過(guò)程與方法進(jìn)行詳細(xì)全面的實(shí)例講解。具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12
jQuery EasyUI之驗(yàn)證框validatebox實(shí)例詳解
本文通過(guò)實(shí)例代碼給大家講解了jQuery EasyUI之驗(yàn)證框validatebox知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-04-04
鼠標(biāo)經(jīng)過(guò)出現(xiàn)氣泡框的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇鼠標(biāo)經(jīng)過(guò)出現(xiàn)氣泡框的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
jquery實(shí)現(xiàn)列表上下移動(dòng)功能
這篇文章主要為大家詳細(xì)介紹了jquery 實(shí)現(xiàn)列表上移、下移功能的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-02-02
Expandable "Detail" Table Rows
Expandable "Detail" Table Rows...2007-08-08

