Home >Web Front-end >JS Tutorial >How to implement simple paging function using js? (code example)

How to implement simple paging function using js? (code example)

藏色散人
藏色散人Original
2018-08-06 17:27:204820browse

The paging function is basically indispensable for website construction. So how to use js to implement simple paging function? For novice or non-experienced programmers, this article's introduction to the implementation of js paging function may be helpful.

js implementation of simple paging code examples are as follows:

1.HTML code:

<div class=" " id="pagecontrol"> 
                                <ul>
                                    <li><span id="prepage">上一页</span></li>
                                    <li class="num current"><a>1</a></li>
                                    <li class="num"><a>2</a></li>
                                    <li class="num"><a>3</a></li>
                                    <li class="num"><a>4</a></li>
                                    <li class="num"><a>5</a></li>
                                    <li><span id="nextpage">下一页<span></li>
                                </ul>
                            </div>

2.js Code:

$(function(){
        var curpage=1;//当前页码
        var maxpage = 10;//最大页码
        
        if(maxpage > 1)
            $("#nextpage").addClass("active");
        
        $("#rowsToshow").change(function(){
            console.log($("#rowsToshow").val());
        })
        $("#prepage").live("click",function(){
            curpage = curpage - 1;
            pageshow(curpage,maxpage);
        })
        $("#nextpage").live("click",function(){
            curpage = curpage + 1;
            pageshow(curpage,maxpage);
        })
        $("#pagecontrol li a").live("click",function(){
            curpage = Number($(this).text());
            pageshow(curpage,maxpage);
        })
    })
    
    function pageshow(cp,tp){
        
        var sp = cp - 2;//startpage
        var ep = cp + 2;//endpage
        var eoff = ep - tp;//tp:totalpage
        if(eoff>0){
                sp = sp - eoff;
        }
        if(sp<=0){
            ep = ep -sp + 1;
        }
        var str = &#39;&#39;;
        if(cp != 1)
            str = str + &#39;<ul><li><span id="prepage" class="active">上一页</span></li>&#39;
        else
            str = str + &#39;<ul><li><span id="prepage">上一页</span></li>&#39;
        for(var i= sp;i<=ep;i++){
            if(i>0&& i<=tp){
                if(i == cp)
                    str = str + &#39;<li class="num current"><a>&#39;+i+&#39;</a></li>&#39;;
                else
                    str = str + &#39;<li class="num"><a>&#39;+i+&#39;</a></li>&#39;;
            }
        }
        
        if(cp != tp)
            str = str + &#39;<li><span class="active" id="nextpage">下一页<span></li></ul>&#39;;
        else
            str = str + &#39;<li><span id="nextpage">下一页<span></li></ul>&#39;;
        $("#pagecontrol").html(str);
    }

How to implement simple paging function using js? (code example)

[Related article recommendations]

Example of php to achieve paging effect

How to use PHP to implement list paging function

Use PHP to implement simple paging class and its detailed usage

javascript to implement paging function


The above is the detailed content of How to implement simple paging function using js? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn