代码改变世界

js分页算法

2015-11-26 09:41  danson  阅读(3173)  评论(0编辑  收藏  举报

function get_hs_page(cur_page, total_page) {
var result = "";
for(var i = 1; i <= total_page; i++) {
if(i == 2 && cur_page - 6 > 1) {
i = cur_page - 6;
}else if(i == cur_page + 6 && cur_page + 6 < total_page) {
i = total_page - 1;
}else{
if(i == cur_page){
result += "<li><a style='background-color:#e7e7e7;' href='/topic?page="+i+"&type=hs'>"+i+"</a></li>";
}else{
result += "<li><a href='/topic?page="+i+"&type=hs'>"+i+"</a></li>";
}

}
}
return result;
}