//创建分页脚本
//_nPageCount 总页数 _nCurrIndex 当前页 _sPageName页面名 _sPageExt 页面后缀
function createPageHTML(_nPageCount, _nCurrIndex, _sPageName, _sPageExt){
if(_nPageCount == null || _nPageCount<=1){
return;
}
var nCurrIndex = _nCurrIndex || 1;
//输出分页
if(nCurrIndex == 1){
// 1 输出首页和上一页
document.write("<a class='toEnd' style='text-decoration:none'>首页</a><a class='toEnd prePage' style='text-decoration:none'>上一页</a><a class='num on' style='text-decoration:none'>1</a>");
}else{
//1.2 当前页不是首页
var nPreIndex = nCurrIndex - 1;
document.write("<a href=\""+_sPageName+"."+_sPageExt+"\">首页</a>");
document.write("<a class='prePage' href=\"" + _sPageName + "."+_sPageExt+"?currentPage="+nPreIndex+"\">上一页</a>");
document.write("<a class='num' href=\""+_sPageName+"."+_sPageExt+"?currentPage=1\">1</a>");
}
// 2 输出中间分页
for(var i=2; i<=_nPageCount; i++){
if(nCurrIndex == i)
document.write("<a class='num on'>"+i + "</a>");
else
document.write("<a href=\""+_sPageName + "."+_sPageExt+"?currentPage="+i+"\">"+i+"</a>");
}
// 3 输出下一页和尾页
// 3.1 当前页是尾页
if(nCurrIndex == _nPageCount){
document.write("<a class='toEnd nextPage' style='text-decoration:none'>下一页</a><a class='toEnd' style='text-decoration:none'>尾页</a>");
}
// 3.2 当前页不是尾页
else{
var nNextIndex = nCurrIndex + 1;
document.write("<a class='nextPage' href=\""+_sPageName + "."+_sPageExt+"?currentPage="+nNextIndex+"\">下一页</a>");
document.write("<a href=\""+_sPageName+ "."+_sPageExt+"?currentPage="+_nPageCount+"\">尾页</a>");
}
}
在jsp页面需要添加分页的地方添加如下的代码:
<div class="pageNav clearfix">
<SCRIPT LANGUAGE="JavaScript">
createPageHTML(${totalPage}, ${currentPage}, "to_anns", "html");
</SCRIPT>
</div>
添加css
.pageNav {text-align: center;margin-top: 27px;}
.pageNav a.toEnd {color: #999;}
.pageNav .prePage {margin-right: 23px;}
element.style {text-decoration: none;}
.pageNav a.on {color: #E05717;font-weight: bold;}
.pageNav a {padding: 6px;}
.pageNav a.num {color: #666;}
.pageNav .nextPage {margin-left: 23px;}
解释下createPageHTML这个方法的四个参数
①_nPageCount:总页数,从action中传过来
②_nCurrIndex:当前的页码
③_sPageName:action的名字
④_sPageExt:action的后缀