js动态生成页码,只显示五个页码

html代码:


<div id="pageNumberDiv">
	<ul class="pagination">ul>
div>

js代码:

function initPage(pageInfo){
	//清除掉之前的页码
	$(".pagination li").remove();
	
	var total = pageInfo.allPageNum;//总页数
	var pageNum = pageInfo.curPageNum;//当前页
	var firstPage = 1;//第一页
	var prePage = pageNum - 1;//上一页
	var nextPage = pageNum + 1;//下一页
	var lastPage = total;
    
    // 先拼接点击事件   ,用于解决字符串拼接的问题 
	var firstPageStr = "retrieve(1)";
	var prePageStr = "retrieve("+prePage+")";
	var nextPageStr = "retrieve("+nextPage+")";
	var lastPageStr = "retrieve("+lastPage+")";
	
	
    //做判断,满足某些条件时,不能点击
	if(firstPage==pageNum){
		$(".pagination").append("
  • 首页
  • "
    ); }else{ $(".pagination").append("
  • +firstPageStr+">首页
  • "
    ); } if(prePage<=0){ $(".pagination").append("
  • 上一页
  • "
    ); }else{ $(".pagination").append("
  • +prePageStr+">上一页
  • "
    ); } //生成中间的页面 if(pageNum<=3){ for(var i=1; i<=(total<=5?total:5); i++){ if(i==pageNum){ $(".pagination").append("
  • "+i+"
  • "
    ); }else{ var pageNumber = "retrieve("+i+")"; $(".pagination").append("
  • +pageNumber+">"+ i+ "
  • "
    ); } } }else if(pageNum>3 && total<=(pageNum+2)){ for(var i=total-4; i<=total; i++){ if(i==pageNum){ $(".pagination").append("
  • "+i+"
  • "
    ); }else{ var pageNumber = "retrieve("+i+")"; $(".pagination").append("
  • +pageNumber+">"+ i+ "
  • "
    ); } } }else if(pageNum>3 && total>(pageNum+2)){ for(var i=pageNum-2; i<=pageNum+2; i++){ if(i==pageNum){ $(".pagination").append("
  • "+i+"
  • "
    ); }else{ var pageNumber = "retrieve("+i+")"; $(".pagination").append("
  • +pageNumber+">"+ i+ "
  • "
    ); } } } if(nextPage<=0 || nextPage > total){ $(".pagination").append("
  • 下一页
  • "
    ); }else{ $(".pagination").append("
  • +nextPageStr+">下一页
  • "
    ); } if(lastPage==pageNum){ $(".pagination").append("
  • 尾页
  • "
    ); }else{ $(".pagination").append("
  • +lastPageStr+">尾页
  • "
    ); } }

    你可能感兴趣的:(前端)