jquery自动生成分页控件 - pagetest.js
/* pagenum:当前页数 theallnum:总条数 themeiye:每页显示多少条 */ function pagetest(pagenum, theallnum, themeiye) { $("#pages").html(""); var allnum = theallnum; //总页数 var meiye = themeiye; //每页显示条数 var xsnumber = 6; var cjnum = Math.ceil(allnum / meiye); var str = ""; var activepage = pagenum; var prevpage = parseInt(activepage) - 1; var nextpage = parseInt(activepage) + 1; if (activepage != 1) { str = '<a data-href="' + prevpage + '">上一页</a>' } if (cjnum > 6) { if (pagenum > 3) { if (pagenum + 2 < cjnum) { for (var i = pagenum - (xsnumber - 4) ; i <= pagenum + (xsnumber - 4) ; i++) { loadpage(i); } str += "..."; str += '<a data-href="' + cjnum + '">' + cjnum + '</a>'; } else { for (var j = 1; j <= activepage - (cjnum - (xsnumber - 3)) ; j++) { loadpage(j); } str += '<span>...</span>'; for (var i = activepage - 2; i <= cjnum; i++) { loadpage(i); } } } else { for (var i = 1; i < xsnumber; i++) { loadpage(i); } str += '<span>...</span>'; str += '<a data-href="' + cjnum + '">' + cjnum + '</a>'; } if (activepage != cjnum) { str += '<a data-href="' + nextpage + '">下一页</a>' }; $("#pages").append(str); } else { for (var i = 1; i <= cjnum; i++) { loadpage(i); } if (activepage != cjnum) { str += '<a data-href="' + nextpage + '">下一页</a>' console.log(nextpage) }; $("#pages").append(str); //countMaxH(); } function loadpage(i) { i == activepage ? str += '<a class="active" data-href="' + i + '">' + i + '</a>' : str += '<a data-href="' + i + '">' + i + '</a>'; } };