上网看新闻或是资料时,经常看到有些文章内容分页。非常好奇其实现方法。今天在网上淘到一个非常棒的用javasript给页面内容分页的例子。看下效果吧 :
下面是代码,大家可以研究一些,非常不错的javascript代码:
1 None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
2 None.gif < html >
3 None.gif < head >
4 None.gif < title > New Document </ title >
5 None.gif < meta name ="Generator" content ="EditPlus" />
6 None.gif < meta name ="Author" content ="" />
7 None.gif < meta name ="Keywords" content ="" />
8 None.gif < meta name ="Description" content ="" />
9 ExpandedBlockStart.gifContractedBlock.gif < style type ="text/css" > dot.gif
10ExpandedSubBlockStart.gifContractedSubBlock.gif* {dot.gif}{
11InBlock.giffont-size:10.2pt;
12InBlock.giffont-family:tahoma;
13InBlock.gifline-height:150%;
14ExpandedSubBlockEnd.gif}
15InBlock.gif.divContent
16ExpandedSubBlockStart.gifContractedSubBlock.gif{dot.gif}{
17InBlock.gifborder:1px solid red;
18InBlock.gifbackground-color:#FFD2D3;
19InBlock.gifwidth:500px;
20InBlock.gifword-break:break-all;
21InBlock.gifmargin:10px 0px 10px;
22InBlock.gifpadding:10px;
23ExpandedBlockEnd.gif}
24None.gif </ style >
25 None.gif </ head >
26 None.gif < body >
27 None.gif < span > header </ span >
28 None.gif
29 None.gif < div id ="divContent" >
30 None.gif </ div >
31 None.gif < div id ="divPagenation" >
32 None.gif </ div >
33 None.gif < span > footer </ span >
34 None.gif
35 ExpandedBlockStart.gifContractedBlock.gif < script language ="JavaScript" type ="text/javascript" > dot.gif
36InBlock.gif<!--
37InBlock.gifs="<p>女老师竭力向孩子们证明,学习好功课的重要性。<ul><li>aaa</li><li>bbb</li></ul> </p><p>她说:“牛顿坐在树下,眼睛盯着树在思考,这时,有一个苹果落在他的头上,于是他发现了万有引力定律,孩子们,你们想想看,做一位伟大的科学家多么好,多么神气啊,要想做到这一点,就必须好好学习。” </p><p>“班上一个调皮鬼对此并不满意。他说:“兴许是这样,可是,假如他坐在学校里,埋头书本,那他就什么也发现不了啦。” </p><p>女老师竭力向孩子们证明,学习好功课的重要性。 </p><p>她说:“牛顿坐在树下,眼睛盯着树在思考,这时,有一个苹果落在他的头上,于是他发现了万有引力定律,孩子们,你们想想看,做一位伟大的科学家多么好,多么神气啊,要想做到这一点,就必须好好学习。” </p><p>“班上一个调皮鬼对此并不满意。他说:“兴许是这样,可是,假如他坐在学校里,埋头书本,那他就什么也发现不了啦。” </p><p>女老师竭力向孩子们证明,学习好功课的重要性。 </p><p>她说:“牛顿坐在树下,眼睛盯着树在思考,这时,有一个苹果落在他的头上,于是他发现了万有引力定律,孩子们,你们想想看,做一位伟大的科学家多么好,多么神气啊,要想做到这一点,就必须好好学习。” </p><p>“班上一个调皮鬼对此并不满意。他说:“兴许是这样,可是,假如他坐在学校里,埋头书本,那他就什么也发现不了啦。” </p><p>女老师竭力向孩子们证明,学习好功课的重要性。 </p><p>她说:“牛顿坐在树下,眼睛盯着树在思考,这时,有一个苹果落在他的头上,于是他发现了万有引力定律,孩子们,你们想想看,做一位伟大的科学家多么好,多么神气啊,要想做到这一点,就必须好好学习。” </p><p>“班上一个调皮鬼对此并不满意。他说:“兴许是这样,可是,假如他坐在学校里,埋头书本,那他就什么也发现不了啦。” </p><p>女老师竭力向孩子们证明,学习好功课的重要性。 </p><p>她说:“牛顿坐在树下,眼睛盯着树在思考,这时,有一个苹果落在他的头上,于是他发现了万有引力定律,孩子们,你们想想看,做一位伟大的科学家多么好,多么神气啊,要想做到这一点,就必须好好学习。” </p><p>“班上一个调皮鬼对此并不满意。他说:“兴许是这样,可是,假如他坐在学校里,埋头书本,那他就什么也发现不了啦。” </p>";
38InBlock.gif
39InBlock.giffunction DHTMLpagenation(content)
40ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
41InBlock.gif with (this)
42ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
43InBlock.gif // client static html file pagenation
44InBlock.gif
45InBlock.gif this.content=content;
46InBlock.gif this.contentLength=content.length;
47InBlock.gif this.pageSizeCount;
48InBlock.gif this.perpageLength=100; //default perpage byte length.
49InBlock.gif this.currentPage=1;
50InBlock.gif //this.regularExp=/.+[\?\&]{1}page=(\d+)/;
51InBlock.gif this.regularExp=/\d+/;
52InBlock.gif
53InBlock.gif this.divDisplayContent;
54InBlock.gif this.contentStyle=null;
55InBlock.gif this.strDisplayContent="";
56InBlock.gif this.divDisplayPagenation;
57InBlock.gif this.strDisplayPagenation="";
58InBlock.gif
59InBlock.gif arguments.length==2?perpageLength=arguments[1]:'';
60InBlock.gif
61ExpandedSubBlockStart.gifContractedSubBlock.gif try dot.gif{
62InBlock.gif divExecuteTime=document.createElement("DIV");
63InBlock.gif document.body.appendChild(divExecuteTime);
64ExpandedSubBlockEnd.gif }
65InBlock.gif catch(e)
66ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
67ExpandedSubBlockEnd.gif }
68InBlock.gif if(document.getElementById("divContent"))
69ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
70InBlock.gif divDisplayContent=document.getElementById("divContent");
71ExpandedSubBlockEnd.gif }
72InBlock.gif else
73ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
74InBlock.gif try
75ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
76InBlock.gif divDisplayContent=document.createElement("DIV");
77InBlock.gif divDisplayContent.id="divContent";
78InBlock.gif document.body.appendChild(divDisplayContent);
79ExpandedSubBlockEnd.gif }
80InBlock.gif catch(e)
81ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
82InBlock.gif return false;
83ExpandedSubBlockEnd.gif }
84ExpandedSubBlockEnd.gif }
85InBlock.gif
86InBlock.gif if(document.getElementById("divPagenation"))
87ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
88InBlock.gif divDisplayPagenation=document.getElementById("divPagenation");
89ExpandedSubBlockEnd.gif }
90InBlock.gif else
91ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
92InBlock.gif try
93ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
94InBlock.gif divDisplayPagenation=document.createElement("DIV");
95InBlock.gif divDisplayPagenation.id="divPagenation";
96InBlock.gif document.body.appendChild(divDisplayPagenation);
97ExpandedSubBlockEnd.gif }
98InBlock.gif catch(e)
99ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
100InBlock.gif return false;
101ExpandedSubBlockEnd.gif }
102ExpandedSubBlockEnd.gif }
103InBlock.gif
104InBlock.gif DHTMLpagenation.initialize();
105InBlock.gif return this;
106InBlock.gif
107ExpandedSubBlockEnd.gif }
108ExpandedSubBlockEnd.gif};
109InBlock.gif
110InBlock.gifDHTMLpagenation.initialize=function()
111ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
112InBlock.gif with (this)
113ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
114InBlock.gif divDisplayContent.className=contentStyle!=null?contentStyle:"divContent";
115InBlock.gif if(contentLength<=perpageLength)
116ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
117InBlock.gif strDisplayContent=content;
118InBlock.gif divDisplayContent.innerHTML=strDisplayContent;
119InBlock.gif return null;
120ExpandedSubBlockEnd.gif }
121InBlock.gif
122InBlock.gif pageSizeCount=Math.ceil((contentLength/perpageLength));
123InBlock.gif
124InBlock.gif DHTMLpagenation.goto(currentPage);
125InBlock.gif DHTMLpagenation.displayContent();
126ExpandedSubBlockEnd.gif }
127ExpandedSubBlockEnd.gif};
128InBlock.gif
129InBlock.gifDHTMLpagenation.displayPage=function()
130ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
131InBlock.gif with (this)
132ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
133InBlock.gif strDisplayPagenation="分页:";
134InBlock.gif
135InBlock.gif if(currentPage&¤tPage!=1)
136InBlock.gif strDisplayPagenation+='<a href="javascript:void(0)" onclick="DHTMLpagenation.previous()">上一页</a> ';
137InBlock.gif else
138InBlock.gif strDisplayPagenation+="上一页 ";
139InBlock.gif
140InBlock.gif for(var i=1;i<=pageSizeCount;i++)
141ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
142InBlock.gif if(i!=currentPage)
143InBlock.gif strDisplayPagenation+='<a href="javascript:void(0)" onclick="DHTMLpagenation.goto('+i+');">'+i+'</a> ';
144InBlock.gif else
145InBlock.gif strDisplayPagenation+=i+" ";
146ExpandedSubBlockEnd.gif }
147InBlock.gif
148InBlock.gif if(currentPage&¤tPage!=pageSizeCount)
149InBlock.gif strDisplayPagenation+='<a href="javascript:void(0)" onclick="DHTMLpagenation.next()">下一页</a> ';
150InBlock.gif else
151InBlock.gif strDisplayPagenation+="下一页 ";
152InBlock.gif
153InBlock.gif strDisplayPagenation+="共 " + pageSizeCount + " 页,每页" + perpageLength + " 字符,调整字符数:<input type='text' value='"+perpageLength+"' id='ctlPerpageLength'><input type='button' value='确定' οnclick='DHTMLpagenation.change(document.getElementById(\"ctlPerpageLength\").value);'>";
154InBlock.gif
155InBlock.gif divDisplayPagenation.innerHTML=strDisplayPagenation;
156ExpandedSubBlockEnd.gif }
157ExpandedSubBlockEnd.gif};
158InBlock.gifDHTMLpagenation.previous=function()
159ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
160InBlock.gif with(this)
161ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
162InBlock.gif DHTMLpagenation.goto(currentPage-1);
163ExpandedSubBlockEnd.gif }
164ExpandedSubBlockEnd.gif};
165InBlock.gifDHTMLpagenation.next=function()
166ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
167InBlock.gif with(this)
168ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
169InBlock.gif DHTMLpagenation.goto(currentPage+1);
170ExpandedSubBlockEnd.gif }
171ExpandedSubBlockEnd.gif};
172InBlock.gifDHTMLpagenation.goto=function(iCurrentPage)
173ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
174InBlock.gif with (this)
175ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
176InBlock.gif startime=new Date();
177InBlock.gif if(regularExp.test(iCurrentPage))
178ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
179InBlock.gif currentPage=iCurrentPage;
180InBlock.gif strDisplayContent=content.substr((currentPage-1)*perpageLength,perpageLength);
181ExpandedSubBlockEnd.gif }
182InBlock.gif else
183ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
184InBlock.gif alert("page parameter error!");
185ExpandedSubBlockEnd.gif }
186InBlock.gif DHTMLpagenation.displayPage();
187InBlock.gif DHTMLpagenation.displayContent();
188ExpandedSubBlockEnd.gif }
189ExpandedSubBlockEnd.gif};
190InBlock.gifDHTMLpagenation.displayContent=function()
191ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
192InBlock.gif with (this)
193ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
194InBlock.gif divDisplayContent.innerHTML=strDisplayContent;
195ExpandedSubBlockEnd.gif }
196ExpandedSubBlockEnd.gif};
197InBlock.gifDHTMLpagenation.change=function(iPerpageLength)
198ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
199InBlock.gif with(this)
200ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
201InBlock.gif if(regularExp.test(iPerpageLength))
202ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
203InBlock.gif DHTMLpagenation.perpageLength=iPerpageLength;
204InBlock.gif DHTMLpagenation.currentPage=1;
205InBlock.gif DHTMLpagenation.initialize();
206ExpandedSubBlockEnd.gif }
207InBlock.gif else
208ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
209InBlock.gif alert("请输入数字");
210ExpandedSubBlockEnd.gif }
211ExpandedSubBlockEnd.gif }
212ExpandedSubBlockEnd.gif};
213InBlock.gif
214InBlock.gif// method
215InBlock.gif// DHTMLpagenation(strContent,perpageLength)
216InBlock.gif
217InBlock.gifDHTMLpagenation(s,100);
218InBlock.gif
219InBlock.gif//-->
220ExpandedBlockEnd.gif </ script >
221 None.gif
222 None.gif </ body >
223 None.gif </ html >
224 None.gif