基于jquery編寫分頁(yè)插件
更新時(shí)間:2016年03月07日 11:26:36 作者:正文
這篇文章主要為大家詳細(xì)介紹了基于jquery編寫分頁(yè)插件的相關(guān)資料,感興趣的小伙伴們可以參考一下
擴(kuò)展JQuery很容易,作為一個(gè)練習(xí),編寫一個(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í)有所幫助。
相關(guān)文章
基于jQuery+Cookie實(shí)現(xiàn)的防止刷新的在線考試倒計(jì)時(shí)
這篇文章主要介紹了基于jQuery+Cookie實(shí)現(xiàn)的防止刷新的在線考試倒計(jì)時(shí)的方法和示例,有需要的小伙伴可以參考下2015-06-06
jQuery獲取Radio,CheckBox選擇的Value值(示例代碼)
這篇文章主要是對(duì)jQuery獲取Radio,CheckBox選擇的Value值進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12
jQuery 中使用JSON的實(shí)現(xiàn)代碼
json 是 Ajax 中使用頻率最高的數(shù)據(jù)格式,在瀏覽器和服務(wù)器中之間的通訊可離不開它2011-12-12
Jquery在IE7下無(wú)法使用 $.ajax解決方法
今天在做系統(tǒng)測(cè)試的時(shí)候,原本用Jquery寫了一個(gè)動(dòng)態(tài)加載的樹形菜單,發(fā)現(xiàn)在IE7下無(wú)法加載數(shù)據(jù),(采用的是jquery1.3.2版本的$.ajax方法),上網(wǎng)查詢到原來(lái)是IE7的執(zhí)行ajax是用XMLHTTPRequest來(lái)聲明的,經(jīng)過(guò)對(duì)比果然如此;后采用以下的方法隨即解決了問(wèn)題。2009-11-11
jQuery實(shí)現(xiàn)伸展與合攏panel的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)伸展與合攏panel的方法,可實(shí)現(xiàn)操作div層的平滑收縮與展開的功能,涉及jQuery中next、slideUp、slideDown等方法的使用技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-04-04

