Home > Web Front-end > JS Tutorial > body text

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

藏色散人
Release: 2018-08-06 17:27:20
Original
4787 people have browsed it

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>
Copy after login

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);
    }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!