• js 实现的前端分页功能


    <!DOCTYPE html>
    <html>
      <head>
        <title>t.html</title>
        <meta name="content-type" content="text/html; charset=GB2312">
        <meta name="keywords" content="keyword1,keyword2,keyword3">
        <meta name="description" content="this is my page">
        <script src="js/jquery.js" ></script>
      </head>
      <style>
          span{
              margin-right:10px;
          }
      </style>
    <body>
        <div id="demo">
            <ul id='id1'>
                <li>1</li>
                <li>1</li>
                <li>1</li>
                <li>1</li>
                <li>1</li>
                <li>1</li>
                <li>1</li>
                <li>1</li>
                <li>1</li>
                <li>1</li>
                <li>1</li>
            </ul>
            <div id='skip'></div>
        </div>
    </body>
      <script type="text/javascript">
        var pagesize = 5;
        var arr = [],use=false;
        function pager(id,page,pagesize,skipid){
            console.log(page)
            if(!use){
                $(id+">li").each(function(i){
                    arr.push($(this).html());
                });
                use = true;
            }
            console.log(arr);
            var count = arr.length;
            var pagecount = Math.ceil(count/pagesize);
            $(id).html("");
            if(page<1)
                page = 1;
            if(page > pagecount)
                page = pagecount;
            var content = "";
            for(var i=(page*pagesize-pagesize);i<(page*pagesize>count?count:page*pagesize);i++){
                content += "<li>"+arr[i]+"</li>"
            }
            console.log(content);
            $(id).html(content);
            var ye = "";
            for(var j=0;j<pagecount;j++){
                ye += "<span><a href='#' onclick=pager('"+id+"',"+(j+1)+","+pagesize+",'"+skipid+"')>"+(j+1)+"</a></span>";
            }
            var contents = "<span>共"+pagecount+"页/第"+page+"页</span>";
            contents += "<span>"+ye+"</span>";
            contents += "<span><a href='#' onclick=pager('"+id+"',"+(page-1)+","+pagesize+",'"+skipid+"')>&lt;&lt;</a></span>";
            contents += "<span><a href='#' onclick=pager('"+id+"',"+(page+1)+","+pagesize+",'"+skipid+"')>&gt;&gt;</a></span>";
            $(skipid).html(contents);
        }
        pager("#id1",0,pagesize,"#skip");
      </script>
    </html>

    tips:以上代码只限于一次调用,多次调用存在bug,改良版:

    <!DOCTYPE html>
    <html>
    <head>
    <title>t.html</title>
    <meta name="content-type" content="text/html; charset=GB2312">
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <script src="js/jquery.js" ></script>
    </head>
    <style>
    span{
    margin-right:10px;
    }
    </style>
    <body>
    <div id="demo0">
    <ul id='id0'>
    <li>1</li>
    <li>1</li>
    <li>1</li>
    <li>1</li>
    <li>1</li>
    <li>1</li>
    <li>1</li>
    <li>1</li>
    <li>1</li>
    <li>1</li>
    <li>1</li>
    </ul>
    <div id='skip0'></div>
    </div>
    <div id="demo1">
    <ul id='id1'>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    </ul>
    <div id='skip1'></div>
    </div>
    </body>
    <script type="text/javascript">

    
    

    var pagesize = 5;
    var json = {};
    function pager(id,page,pagesize,skipid){
    if(json[id]==undefined||json[id]['use']==false||json[id]['use']==undefined){
    var arr = [],jsonitem={};
    $(id+">li").each(function(i){
    arr.push($(this).html());
    });
    jsonitem["arr"]=arr;
    jsonitem["use"]= true;
    json[id]=jsonitem;
    }
    console.log(json)
    var arr = json[id]['arr'];
    console.log(arr);
    var count = arr.length;
    var pagecount = Math.ceil(count/pagesize);
    $(id).html("");
    if(page<1)
    page = 1;
    if(page > pagecount)
    page = pagecount;
    var content = "";
    for(var i=(page*pagesize-pagesize);i<(page*pagesize>count?count:page*pagesize);i++){
    content += "<li>"+arr[i]+"</li>"
    }
    console.log(content);
    $(id).html(content);
    var ye = "";
    for(var j=0;j<pagecount;j++){
    ye += "<span><a href='#' onclick=pager('"+id+"',"+(j+1)+","+pagesize+",'"+skipid+"')>"+(j+1)+"</a></span>";
    }
    var contents = "<span>共"+pagecount+"页/第"+page+"页</span>";
    contents += "<span>"+ye+"</span>";
    contents += "<span><a href='#' onclick=pager('"+id+"',"+(page-1)+","+pagesize+",'"+skipid+"')>&lt;&lt;</a></span>";
    contents += "<span><a href='#' onclick=pager('"+id+"',"+(page+1)+","+pagesize+",'"+skipid+"')>&gt;&gt;</a></span>";
    $(skipid).html(contents);
    }
    pager("#id0",0,pagesize,"#skip0");
    pager("#id1",0,pagesize,"#skip1");
    </script>
    </html>

     
  • 相关阅读:
    JavaScript 学习笔记 事件二
    auto_ptr
    POJ2299 UltraQuickSort(逆序对个数)
    2016年11月2日22:28:14
    将sql server中的数据倒入Excel(c#)
    线段树成段更新裸题POJ3468
    线段树成断更新裸题hdu1698 Just a Hook
    POJ2828 思维难度较好的一道线段树
    < 弱牛刷贪心给JerryDung&qiuwei大神Orz>最大乘积
    NOIP 2008 传纸条题解[双线程DP]
  • 原文地址:https://www.cnblogs.com/g177w/p/12931598.html
Copyright © 2020-2023  润新知