Home >Web Front-end >JS Tutorial >How to implement simple paging function using js? (code example)
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 = ''; if(cp != 1) str = str + '<ul><li><span id="prepage" class="active">上一页</span></li>' else str = str + '<ul><li><span id="prepage">上一页</span></li>' for(var i= sp;i<=ep;i++){ if(i>0&& i<=tp){ if(i == cp) str = str + '<li class="num current"><a>'+i+'</a></li>'; else str = str + '<li class="num"><a>'+i+'</a></li>'; } } if(cp != tp) str = str + '<li><span class="active" id="nextpage">下一页<span></li></ul>'; else str = str + '<li><span id="nextpage">下一页<span></li></ul>'; $("#pagecontrol").html(str); }
[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!