网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 基于jquery编写分页插件
内容
    扩展JQuery很容易,作为一个练习,编写一个简单的分页插件,代码量不大,直接看代码好了:
    $.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></a></td>'
            +'<td><a href="#"><img></a></td>'
            +'<td><span>第<input type="text">页/共'+numPages()+'页</span></td>'
            +'<td><a href="#"><img></a></td>'
            +'<td><a href="#"><img></a></td>'
            +'<td><img src="images/nowait.gif"></td>'
            +'<td><span>检索到'+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(); 
      }); 
    } 
    下面测试一下:
    <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"></div> 
    运行效果图如下:
    名单
    以上就是本文的全部内容,希望对大家的学习有所帮助。
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/15 8:44:25