文章内容分页实例代码
发布日期:2008-06-12最近更新:2008-06-12来源:BHCODE作者:
|
以下是代码片段:
<style> * { font-size:10.2pt; font-family:tahoma; line-height:150%; } .divContent { border:1px solid red; background-color:#FFD2D3; width:500px; word-break:break-all; margin:10px 0px 10px; padding:10px; } </style> header <div id="divPagenation"></div> <div id="divContent"></div> footer <script language="jscript"> <!-- s="一直都没想到一个比较好的长文章分页方法,这里的一个写的还算可以,不过这能通过字数来分页,觉得不是太好,尤其是中英文在一起的时候就不准了,而且有时候会把html标签分开。鉴于此我把这个分页程序改造了一下,通过行数来分页,确实成功了,开始觉得还不错,每页的行数相同,想起来还是比较完美的,不过用过之后发现还是有问题,不如文本软件比如word,txt中的自动换行无法识别出来,这个比较郁闷,不过只有很少行会出问题,还是不错的,但还有个很大的问题,如果文章是图文混排的就会有大问题了,因为图片没办法算到行数里面,哎,失败。。。。写到这里又有一个想法,不知道可行不,以后有时间试试看,我想文章分页时主要是为了固定文章的高度,我想是不是可以用一个固定高度的表格,然后把文章填充到这个表格中,判断是否填充满了,如果满了,就进行分页,关键在于怎么判断表格是否填充满了,可以设置表示是不可挤高的(这个可以通过css样式来实现),然后在进行判断,这个还没想好,先写到这里,有时间在想想吧"; function DHTMLpagenation(content) { with (this) { // client static html file pagenation this.content=content; this.contentLength=content.length; this.pageSizeCount; this.perpageLength=100; //default perpage byte length. this.currentPage=1; //this.regularExp=/.+[\?\&]page=(\d+)/; this.regularExp=/\d+/; this.divDisplayContent; this.contentStyle=null; this.strDisplayContent=""; this.divDisplayPagenation; this.strDisplayPagenation=""; arguments.length==2?perpageLength=arguments[1]:''; try { divExecuteTime=document.createElement("DIV"); document.body.appendChild(divExecuteTime); } catch(e) { } if(document.getElementById("divContent")) { divDisplayContent=document.getElementById("divContent"); } else { try { divDisplayContent=document.createElement("DIV"); divDisplayContent.id="divContent"; document.body.appendChild(divDisplayContent); } catch(e) { return false; } } if(document.getElementById("divPagenation")) { divDisplayPagenation=document.getElementById("divPagenation"); } else { try { divDisplayPagenation=document.createElement("DIV"); divDisplayPagenation.id="divPagenation"; document.body.appendChild(divDisplayPagenation); } catch(e) { return false; } } DHTMLpagenation.initialize(); return this; }}; DHTMLpagenation.initialize=function() { with (this) { divDisplayContent.className=contentStyle!=null?contentStyle:"divContent"; if(contentLength<=perpageLength) { strDisplayContent=content; divDisplayContent.innerHTML=strDisplayContent; return null; } pageSizeCount=Math.ceil((contentLength/perpageLength)); DHTMLpagenation.goto(currentPage); DHTMLpagenation.displayContent(); }}; DHTMLpagenation.displayPage=function() { with (this) { strDisplayPagenation="分页:"; if(currentPage&¤tPage!=1) strDisplayPagenation+='<a href="javascript:void(0)" onclick="DHTMLpagenation.previous()">上一页</a> '; else strDisplayPagenation+="上一页 "; for(var i=1;i<=pageSizeCount;i++) { if(i!=currentPage) strDisplayPagenation+='<a href="javascript:void(0)" onclick="DHTMLpagenation.goto('+i+');">'+i+'</a> '; else strDisplayPagenation+=i+" "; } if(currentPage&¤tPage!=pageSizeCount) strDisplayPagenation+='<a href="javascript:void(0)" onclick="DHTMLpagenation.next()">下一页</a> '; else strDisplayPagenation+="下一页 "; strDisplayPagenation+="共 " + pageSizeCount + " 页,每页" + perpageLength + " 字符,调整字符数:<input type='text' value='"+perpageLength+"' id='ctlPerpageLength'><input type='button' value='确定' onclick='DHTMLpagenation.change(document.getElementById(\"ctlPerpageLength\").value);'>"; divDisplayPagenation.innerHTML=strDisplayPagenation; }}; DHTMLpagenation.previous=function() { with(this) { DHTMLpagenation.goto(currentPage-1); }}; DHTMLpagenation.next=function() { with(this) { DHTMLpagenation.goto(currentPage+1); }}; DHTMLpagenation.goto=function(iCurrentPage) { with (this) { startime=new Date(); if(regularExp.test(iCurrentPage)) { currentPage=iCurrentPage; strDisplayContent=content.substr((currentPage-1)*perpageLength,perpageLength); } else { alert("page parameter error!"); } DHTMLpagenation.displayPage(); DHTMLpagenation.displayContent(); }}; DHTMLpagenation.displayContent=function() { with (this) { divDisplayContent.innerHTML=strDisplayContent; }}; DHTMLpagenation.change=function(iPerpageLength) { with(this) { if(regularExp.test(iPerpageLength)) { DHTMLpagenation.perpageLength=iPerpageLength; DHTMLpagenation.currentPage=1; DHTMLpagenation.initialize(); } else { alert("请输入数字"); } }}; // method // DHTMLpagenation(strContent,perpageLength) DHTMLpagenation(s,50); //--> </script>
|