基于jquery編寫(xiě)分頁(yè)插件
擴(kuò)展JQuery很容易,作為一個(gè)練習(xí),編寫(xiě)一個(gè)簡(jiǎn)單的分頁(yè)插件,代碼量不大,直接看代碼好了:
$.fn.mypagination = function(totalProperty,opts){
opts = $.extend({
perPage:10,
callback:function(){
}
},opts||{});
return this.each(function(){
function numPages(){
return Math.ceil(totalProperty/opts.perPage);
}
function selectPage(page){
return function(){
currPage = page;
if (page<0) currPage = 0;
if (page>=numPages()) currPage = numPages()-1;
render();
$('img.page-wait',panel).attr('src','images/wait.gif');
opts.callback(currPage+1);
$('img.page-wait',panel).attr('src','images/nowait.gif');
}
}
function render(){
var html = '<table><tbody><tr>'
+'<td><a href="#"><img class="page-first"></a></td>'
+'<td><a href="#"><img class="page-prev"></a></td>'
+'<td><span>第<input type="text" class="page-num">頁(yè)/共'+numPages()+'頁(yè)</span></td>'
+'<td><a href="#"><img class="page-next"></a></td>'
+'<td><a href="#"><img class="page-last"></a></td>'
+'<td><img src="images/nowait.gif" class="page-wait"></td>'
+'<td><span style="padding-left:50px;">檢索到'+totalProperty+'記錄</span></td>'
+'</tr></tbody></table>';
var imgFirst = 'images/page-first-disabled.gif';
var imgPrev = 'images/page-prev-disabled.gif';
var imgNext = 'images/page-next-disabled.gif';
var imgLast = 'images/page-last-disabled.gif';
if (currPage > 0){
imgFirst = 'images/page-first.gif';
imgPrev = 'images/page-prev.gif';
}
if (currPage < numPages()-1){
imgNext = 'images/page-next.gif';
imgLast = 'images/page-last.gif';
}
panel.empty();
panel.append(html);
$('img.page-first',panel)
.bind('click',selectPage(0))
.attr('src',imgFirst);
$('img.page-prev',panel)
.bind('click',selectPage(currPage-1))
.attr('src',imgPrev);
$('img.page-next',panel)
.bind('click',selectPage(currPage+1))
.attr('src',imgNext);
$('img.page-last',panel)
.bind('click',selectPage(numPages()-1))
.attr('src',imgLast);
$('input.page-num',panel)
.val(currPage+1)
.change(function(){
selectPage($(this).val()-1)();
});
}
var currPage = 0;
var panel = $(this);
render();
});
}
下面測(cè)試一下:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="mypagination.css"/>
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script type="text/javascript" src="jquery.mypagination.js"></script>
<script>
$(document).ready(function(){
$('#mypage').mypagination(10112,{
callback:function(page){
alert(page);
}
});
});
</script>
</head>
<div id="mypage" class="mypagination"></div>
運(yùn)行效果圖如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- jQuery編寫(xiě)設(shè)置和獲取顏色的插件
- 基于jquery插件編寫(xiě)countdown計(jì)時(shí)器
- jQuery插件編寫(xiě)步驟詳解
- 基于jquery編寫(xiě)的放大鏡插件
- jQuery彈簧插件編寫(xiě)基礎(chǔ)之“又見(jiàn)彈窗”
- 編寫(xiě)自己的jQuery提示框(Tip)插件
- 編寫(xiě)簡(jiǎn)單的jQuery提示插件
- 基于編寫(xiě)jQuery的無(wú)縫滾動(dòng)插件
- Jquery插件編寫(xiě)簡(jiǎn)明教程
- 編寫(xiě)自己的jQuery插件簡(jiǎn)單實(shí)現(xiàn)代碼
- 如何編寫(xiě)jquery插件
相關(guān)文章
jQuery EasyUI API 中文文檔 - NumberSpinner數(shù)值微調(diào)器使用介紹
jQuery EasyUI API 中文文檔 - NumberSpinner數(shù)值微調(diào)器使用,需要的朋友可以參考下。2011-10-10
jQuery 中msgTips 頂部彈窗效果實(shí)現(xiàn)代碼
最近發(fā)現(xiàn)好多網(wǎng)站都采用頂部彈窗,并且不用用戶手動(dòng)去點(diǎn)擊確定。感覺(jué)這樣很方便用戶,下面小編把實(shí)現(xiàn)代碼分享給大家,感興趣的的朋友一起看看吧2017-08-08
jquery checkbox的相關(guān)操作總結(jié)
這篇文章主要介紹了jquery checkbox的相關(guān)操作總結(jié)的相關(guān)資料,需要的朋友可以參考下2016-10-10
jQuery獲取動(dòng)態(tài)添加元素的方法詳解
這篇文章主要介紹了jQuery獲取動(dòng)態(tài)添加元素的方法詳解,jQuery 是一個(gè)高效、精簡(jiǎn)并且功能豐富的 JavaScript 工具庫(kù),它提供的 API 易于使用且兼容眾多瀏覽器,這讓諸如 HTML 文檔遍歷和操作、事件處理、動(dòng)畫(huà)和 Ajax 操作更加簡(jiǎn)單,需要的朋友可以參考下2023-08-08
jQuery網(wǎng)頁(yè)選項(xiàng)卡插件rTabs用法實(shí)例分析
這篇文章主要介紹了jQuery網(wǎng)頁(yè)選項(xiàng)卡插件rTabs用法,較為詳細(xì)的分析了jquery選項(xiàng)卡插件rTabs的定義、實(shí)現(xiàn)及使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
jQuery實(shí)現(xiàn)輸入框的放大和縮小功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)輸入框的放大和縮小功能,涉及jQuery基于事件響應(yīng)的頁(yè)面元素屬性動(dòng)態(tài)變換相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
jQuery實(shí)現(xiàn)的簡(jiǎn)單在線計(jì)算器功能
這篇文章主要介紹了jQuery實(shí)現(xiàn)的簡(jiǎn)單在線計(jì)算器功能,結(jié)合完整實(shí)例形式分析了jQuery實(shí)現(xiàn)簡(jiǎn)單四則運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05

