這篇文章將為大家詳細(xì)講解有關(guān)如何在jquery中使用pagination分頁插件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
目前創(chuàng)新互聯(lián)建站已為1000多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、虎林網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
第一步:引入分頁需要的js(jquery.pagination.js)和css(pagination.css)
pagination插件下載地址
第二步:將分頁條容器寫到頁面里(固定代碼)
第三步:生成分頁條方法是pagination,例如$("#page").pagination(100); 傳入總數(shù)目
例如:
$("#Pagination").pagination(json.data.count, { items_per_page:5, callback:pageselectCallback });
第四步:給分頁加回調(diào),點(diǎn)擊第幾頁的請求寫到回調(diào)函數(shù)里。
回調(diào)函數(shù)帶參數(shù):page_index(頁數(shù)索引,從0開始,第一頁index為0),此值插件自動傳入。
jquery分頁插件pagination示例(ajax應(yīng)用)示例:
javascript代碼:
var sessionStorage = window.sessionStorage; var aid = (JSON.parse(sessionStorage.getItem("user"))).id; var articObj = ""; var articContent = ""; var pageObj = ""; data["pageSize"] = 5; data["pageNow"] = 1; function getMyArtic() { if (userIdid == "aid") { data["aid"] = aid; } else { data["userId"] = aid; } if (document.getElementById("searchTime").value == "") { document.getElementById("searchTime").value = myDate.toLocaleDateString(); } data["createTime"] = document.getElementById("searchTime").value; jQuery.support.cors = true; $.ajax({ url: serverAddress, xhrFields: { withCredentials: true }, crossDomain: true, async: true, cache: false, type: "post", dataType: "json", data: data, success: function(json) { $("#Pagination").empty(); $("#list_container").empty(); $("#searchPage").empty(); if (json.code == 1) { sessionStorage.setItem("artic", JSON.stringify(json.data)); if (json.data.recordCount > 0) { for (var i = 0; i < json.data.recordCount; i++) { articContent = json.data.list[i].content; var regEx = /<[^>]*>/g; articContent = articContent.replace(regEx, ""); if (articContent.length > 100) { articContent = articContent.substring(0, 100) + "..."; } var create_Time = json.data.list[i].createTime; if (create_Time != null || create_Time != "") { create_Time = create_Time.substring(0, 19); } articObj = ""; $("#list_container").append(articObj); // alert(userIdid) var gotoId = "goto" + json.data.list[i].id; if (userIdid == "userId") { //收藏的文章 document.getElementById(gotoId).href = "ArticleDetail.html?id=" + json.data.list[i].id; $(".img_delete").css("display", "none"); } else if (userIdid == "aid") { //我的文章 $(".img_delete").css("display", "block"); document.getElementById(gotoId).href = "editArticle.html?id=" + json.data.list[i].id; } } pageObj = '共' + json.data.totalPage + '頁'; $("#searchPage").append(pageObj); $("#Pagination").pagination(json.data.count, { items_per_page: 5, //pageSize每頁最多顯示多少條 callback: pageselectCallback }); } reSetIframeHeight(); } else if (json.code == 0) {} } }); } window.onload = function() { getMyArtic(); personalityCenterRefresh(); } function pageselectCallback(page_index) { var createTime = document.getElementById("searchTime").value; if (createTime.length == 0 || createTime == null) { createTime = myDate.toLocaleDateString(); } if (userIdid == "aid") { data["aid"] = aid; } else { data["userId"] = aid; } data["pageNow"] = parseInt(page_index) + 1; jQuery.support.cors = true; $.ajax({ url: serverAddress, xhrFields: { withCredentials: true }, crossDomain: true, async: true, cache: false, type: "post", dataType: "json", data: data, success: function(json) { $("#list_container").empty(); if (json.code == 1) { if (json.data.recordCount > 0) { sessionStorage.setItem("artic", JSON.stringify(json.data)); for (var i = 0; i < json.data.recordCount; i++) { articContent = json.data.list[i].content; //alert(articContent); var regEx = /<[^>]*>/g; articContent = articContent.replace(regEx, ""); if (articContent.length > 100) { articContent = articContent.substring(0, 100) + "..."; } var create_Time = json.data.list[i].createTime; if (create_Time != null || create_Time != "") { create_Time = create_Time.substring(0, 19); } articObj = "" + articContent + "" + "" + ""; $("#list_container").append(articObj); } } } else if (json.code == 0) {} } }); }" + articContent + "" + "" + "
html代碼:
關(guān)于如何在jquery中使用pagination分頁插件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。