❶ js的分頁原理以及實現步驟是什麼
主要是借鑒了網上一個例子,修改了一些小地方,前端分頁的技巧,表格的數據是已經寫好了,可以前端渲染表格然後再分頁,都是可以的。
其實分頁最關鍵是這兩句:
var startRow = (currentPage - 1) * pageSize+1; //currentPage 為當前頁,pageSize為每頁顯示的數據量
var endRow = currentPage * pageSize;
找到我們需要顯示的行的范圍(starRow~endRow)
ps:這里在跳轉的時候遇到了一個小BUG,就是獲取到的select的value值是string類型的,比如獲取到了1,然後你想再加1的時候就會變成"11" 而不是我們想要的"2",所以這里需要用parseInt( )來轉換一下,小細節需要注意呀!!!
效果圖:

[javascript] view plain print?
- <!doctypehtml> 
- <html> 
- <head> 
- <metacharset='utf-8'> 
- <styletype="text/css"> 
- a{ 
- text-decoration:none; 
- } 
- .table2{ 
- border:#C8C8C8solid; 
- border-width:1px0px0px1px; 
- background:#F3F0F0; 
- margin-top:25px; 
- } 
- .td0{ 
- border:#C8C8C8solid; 
- border-width:001px0; 
- } 
- .td2{ 
- border:#C8C8C8solid; 
- border-width:01px1px0; 
- } 
- .barcon{ 
- width:1000px; 
- margin:0auto; 
- text-align:center; 
- } 
- .barcon1{ 
- font-size:17px; 
- float:left; 
- margin-top:20px; 
- } 
- .barcon2{ 
- float:right; 
- } 
- .barcon2ul{ 
- margin:20px0; 
- padding-left:0; 
- list-style:none; 
- text-align:center; 
- } 
- .barcon2li{ 
- display:inline; 
- } 
- .barcon2a{ 
- font-size:16px; 
- font-weight:normal; 
- display:inline-block; 
- padding:5px; 
- padding-top:0; 
- color:black; 
- border:1pxsolid#ddd; 
- background-color:#fff; 
- } 
- .barcon2a:hover{ 
- background-color:#eee; 
- } 
- .ban{ 
- opacity:.4; 
- } 
- </style> 
- </head> 
- <body> 
- <tablewidth="950"cellpadding="0"cellspacing="0"class="table2"align="center"> 
- <thead> 
- <tr> 
- <tdcolspan="3"height="33"class="td0"></td> 
- <tdalign="center"class="td2"><ahref="###">添加用戶</a></td> 
- </tr> 
- <tralign="center"> 
- <thwidth="150"height="33"class="td2">序號</th> 
- <thwidth="300"class="td2">用戶名</th> 
- <thwidth="250"class="td2">許可權</th> 
- <thwidth="250"class="td2">操作</th> 
- </tr> 
- </thead> 
- <tbodyid="adminTbody"> 
- <tralign="center"> 
- <tdclass="td2"height="33"width="150">1</td> 
- <tdclass="td2">admin</td> 
- <tdclass="td2">管理員</td> 
- <tdclass="td2"><ahref="###">修改</a></td> 
- </tr> 
- </tbody> 
- </table> 
- <divid="barcon"class="barcon"> 
- <divid="barcon1"class="barcon1"></div> 
- <divid="barcon2"class="barcon2"> 
- <ul> 
- <li><ahref="###"id="firstPage">首頁</a></li> 
- <li><ahref="###"id="prePage">上一頁</a></li> 
- <li><ahref="###"id="nextPage">下一頁</a></li> 
- <li><ahref="###"id="lastPage">尾頁</a></li> 
- <li><selectid="jumpWhere"> 
- </select></li> 
- <li><ahref="###"id="jumpPage"onclick="jumpPage()">跳轉</a></li> 
- </ul> 
- </div> 
- </div> 
- <scriptsrc="jquery.js"></script> 
- <script> 
- /*動態生成用戶函數 
- num為生成的用戶數量 
- */ 
- functiondynamicAddUser(num){ 
- for(vari=1;i<=num;i++) 
- { 
- vartrNode=document.createElement("tr"); 
- $(trNode).attr("align","center"); 
- //序號 
- vartdNodeNum=document.createElement("td"); 
- $(tdNodeNum).html(i+1); 
- tdNodeNum.style.width="150px"; 
- tdNodeNum.style.height="33px"; 
- tdNodeNum.className="td2"; 
- //用戶名 
- vartdNodeName=document.createElement("td"); 
- $(tdNodeName).html("lzj"+i); 
- tdNodeName.style.width="300px"; 
- tdNodeName.className="td2"; 
- //許可權 
- vartdNodePri=document.createElement("td"); 
- tdNodePri.style.width="250px"; 
- tdNodePri.className="td2"; 
- varpriText=document.createElement("span"); 
- $(priText).css({"display":"inline-block","text-align":"center"}); 
- $(priText).text("普通用戶"); 
- tdNodePri.appendChild(priText); 
- //操作 
- vartdNodeOper=document.createElement("td"); 
- tdNodeOper.style.width="170px"; 
- tdNodeOper.className="td2"; 
- vareditA=document.createElement("a"); 
- $(editA).attr("href","###").text("編輯"); 
- $(editA).css({display:"inline-block"}); 
- tdNodeOper.appendChild(editA); 
- trNode.appendChild(tdNodeNum); 
- trNode.appendChild(tdNodeName); 
- trNode.appendChild(tdNodePri); 
- trNode.appendChild(tdNodeOper); 
- $("#adminTbody")[0].appendChild(trNode); 
- } 
- } 
- $(function(){ 
- dynamicAddUser(80); 
- goPage(1,10); 
- vartempOption=""; 
- for(vari=1;i<=totalPage;i++) 
- { 
- tempOption+='<optionvalue='+i+'>'+i+'</option>' 
- } 
- $("#jumpWhere").html(tempOption); 
- }) 
- /** 
- *分頁函數 
- *pno--頁數 
- *psize--每頁顯示記錄數 
- *分頁部分是從真實數據行開始,因而存在加減某個常數,以確定真正的記錄數 
- *純js分頁實質是數據行全部載入,通過是否顯示屬性完成分頁功能 
- **/ 
- varpageSize=0;//每頁顯示行數 
- varcurrentPage_=1;//當前頁全局變數,用於跳轉時判斷是否在相同頁,在就不跳,否則跳轉。 
- vartotalPage;//總頁數 
- functiongoPage(pno,psize){ 
- varitable=document.getElementById("adminTbody"); 
- varnum=itable.rows.length;//表格所有行數(所有記錄數) 
- pageSize=psize;//每頁顯示行數 
- //總共分幾頁 
- if(num/pageSize>parseInt(num/pageSize)){ 
- totalPage=parseInt(num/pageSize)+1; 
- }else{ 
- totalPage=parseInt(num/pageSize); 
- } 
- varcurrentPage=pno;//當前頁數 
- currentPage_=currentPage; 
- varstartRow=(currentPage-1)*pageSize+1; 
- varendRow=currentPage*pageSize; 
- endRow=(endRow>num)?num:endRow; 
- //遍歷顯示數據實現分頁 
- /*for(vari=1;i<(num+1);i++){ 
- varirow=itable.rows[i-1]; 
- if(i>=startRow&&i<=endRow){ 
- irow.style.display=""; 
- }else{ 
- irow.style.display="none"; 
- } 
- }*/ 
- $("#adminTbodytr").hide(); 
- for(vari=startRow-1;i<endRow;i++) 
- { 
- $("#adminTbodytr").eq(i).show(); 
- } 
- vartempStr="共"+num+"條記錄分"+totalPage+"頁當前第"+currentPage+"頁"; 
- document.getElementById("barcon1").innerHTML=tempStr; 
- if(currentPage>1){ 
- $("#firstPage").on("click",function(){ 
- goPage(1,psize); 
- }).removeClass("ban"); 
- $("#prePage").on("click",function(){ 
- goPage(currentPage-1,psize); 
- }).removeClass("ban"); 
- }else{ 
- $("#firstPage").off("click").addClass("ban"); 
- $("#prePage").off("click").addClass("ban"); 
- } 
- if(currentPage<totalPage){ 
- $("#nextPage").on("click",function(){ 
- goPage(currentPage+1,psize); 
- }).removeClass("ban") 
- $("#lastPage").on("click",function(){ 
- goPage(totalPage,psize); 
- }).removeClass("ban") 
- }else{ 
- $("#nextPage").off("click").addClass("ban"); 
- $("#lastPage").off("click").addClass("ban"); 
- } 
- $("#jumpWhere").val(currentPage); 
- } 
- functionjumpPage() 
- { 
- varnum=parseInt($("#jumpWhere").val()); 
- if(num!=currentPage_) 
- { 
- goPage(num,pageSize); 
- } 
- } 
- </script> 
- </body> 
- </html> 
❷ JS 前端的篩選代碼
我特么的終於寫出來了,,給大家拿去用用看,自己添加css樣式。我感覺頭發要掉了。
粗略的樣子就是這樣,後面是代碼。
 //JavaScriptDocument
//JavaScriptDocument
//JavaScriptDocument
/*!jQueryv2.1.4|(c)2005,2015jQueryFoundation,Inc.|jquery.org/license*/
$(document).ready(function(){
			$(".uox").click(function(){
				$(".uox-1").show(500);
				$(".uos-1").hide(500);
				$(".box-1").hide(500);
				$(".jod-1").hide(500);
				$(".sor-1").hide(500);
				$("#page").hide(500);
				
		});
	
		$(".uos").click(function(){
			$(".uos-1").show(500);
				$(".uox-1").hide(500);
				$(".box-1").hide(500);
				$(".jod-1").hide(500);
				$(".sor-1").hide(500);
			$("#page").hide(500);
		});
	
		$(".box").click(function(){
			$(".box-1").show(500);
				$(".uos-1").hide(500);
				$(".uox-1").hide(500);
				$(".jod-1").hide(500);
				$(".sor-1").hide(500);
			$("#page").hide(500);
		});
	
		$(".jod").click(function(){
			$(".jod-1").show(500);
				$(".uos-1").hide(500);
				$(".box-1").hide(500);
				$(".uox-1").hide(500);
				$(".sor-1").hide(500);
			$("#page").hide(500);
		});
	
		$(".sor").click(function(){
			$(".sor-1").show(500);
				$(".uos-1").hide(500);
				$(".box-1").hide(500);
				$(".jod-1").hide(500);
				$(".uox-1").hide(500);
			$("#page").hide(500);
		});
	
	//第一組篩選結束
	//開始第二組篩選
	
	$(".newch").click(function(){
			$(".newch-1").show(500);
			$(".bopch-1").hide(500);
			$(".luoch-1").hide(500);
			$(".dizch-1").hide(500);
			$(".dnych-1").hide(500);
			$(".ousch-1").hide(500);
			$(".tiych-1").hide(500);
			$(".getch-1").hide();
			$("#page").hide(500);
		});
	$(".luoch").click(function(){
			$(".luoch-1").show(500);
		$(".newch-1").hide(500);
			$(".bopch-1").hide(500);
			$(".dizch-1").hide(500);
			$(".dnych-1").hide(500);
			$(".ousch-1").hide(500);
			$(".tiych-1").hide(500);
			$(".getch-1").hide();
			$("#page").hide(500);
		});
	
	$(".bopch").click(function(){
			$(".bopch-1").show(500);
			$(".newch-1").hide(500);
			$(".dizch-1").hide(500);
			$(".dnych-1").hide(500);
			$(".luoch-1").hide(500);
			$(".ousch-1").hide(500);
			$(".tiych-1").hide(500);
			$(".getch-1").hide(500);
			$("#page").hide(500);
		});
	
	
	$(".dizch").click(function(){
			$(".dizch-1").show(500);
			$(".bopch-1").hide(500);
			$(".newch-1").hide(500);
			$(".luoch-1").hide(500);
			$(".dnych-1").hide(500);
			$(".ousch-1").hide(500);
			$(".tiych-1").hide(500);
			$(".getch-1").hide(500);
			$("#page").hide(500);
		});
	
	$(".dnych").click(function(){
			$(".dnych-1").show(500);
			$(".bopch-1").hide(500);
			$(".dizch-1").hide(500);
			$(".luoch-1").hide(500);
			$(".newch-1").hide(500);
			$(".ousch-1").hide(500);
			$(".tiych-1").hide(500);
			$(".getch-1").hide(500);
			$("#page").hide(500);
		});
	
	$(".ousch").click(function(){
			$(".ousch-1").show(500);
			$(".bopch-1").hide(500);
			$(".dizch-1").hide(500);
			$(".luoch-1").hide(500);
			$(".dnych-1").hide(500);
			$(".newch-1").hide(500);
			$(".tiych-1").hide(500);
			$(".getch-1").hide(500);
			$("#page").hide(500);
		});
	
	$(".tiych").click(function(){
			$(".tiych-1").show(500);
			$(".bopch-1").hide(500);
			$(".dizch-1").hide(500);
			$(".luoch-1").hide(500);
			$(".dnych-1").hide(500);
			$(".ousch-1").hide(500);
			$(".newch-1").hide(500);
			$(".getch-1").hide(500);
			$("#page").hide(500);
		});
	
	$(".getch").click(function(){
			$(".getch-1").show(500);
			$(".bopch-1").hide(500);
			$(".luoch-1").hide(500);
			$(".dizch-1").hide(500);
			$(".dnych-1").hide(500);
			$(".ousch-1").hide(500);
			$(".tiych-1").hide(500);
			$(".newch-1").hide(500);
			$("#page").hide(500);
		});
	//第二組結束
	//第三組篩選
	
	$(".wan-10").click(function(){
		$(".wan-sh").show(500);
		$(".wan-es").hide(500);
		$(".wan-ss").hide(500);
		$(".wan-ws").hide(500);
		$(".wan-bs").hide(500);
		$(".wan-yb").hide(500);
		$(".wan-oth").hide(500);
	});
	$(".wan-20").click(function(){
		$(".wan-es").show(500);
		$(".wan-sh").hide(500);
		$(".wan-ss").hide(500);
		$(".wan-ws").hide(500);
		$(".wan-bs").hide(500);
		$(".wan-yb").hide(500);
		$(".wan-oth").hide(500);
	});
	$(".wan-30").click(function(){
		$(".wan-ss").show(500);
		$(".wan-es").hide(500);
		$(".wan-sh").hide(500);
		$(".wan-ws").hide(500);
		$(".wan-bs").hide(500);
		$(".wan-yb").hide(500);
		$(".wan-oth").hide(500);
	});
	$(".wan-50").click(function(){
		$(".wan-ws").show(500);
		$(".wan-es").hide(500);
		$(".wan-ss").hide(500);
		$(".wan-sh").hide(500);
		$(".wan-bs").hide(500);
		$(".wan-yb").hide(500);
		$(".wan-oth").hide(500);
	});
	$(".wan-80").click(function(){
		$(".wan-bs").show(500);
		$(".wan-es").hide(500);
		$(".wan-ss").hide(500);
		$(".wan-ws").hide(500);
		$(".wan-sh").hide(500);
		$(".wan-yb").hide(500);
		$(".wan-oth").hide(500);
	});
	$(".wan-100").click(function(){
		$(".wan-yb").show(500);
		$(".wan-es").hide(500);
		$(".wan-ss").hide(500);
		$(".wan-ws").hide(500);
		$(".wan-bs").hide(500);
		$(".wan-sh").hide(500);
		$(".wan-oth").hide(500);
	});
	$(".wan-ot").click(function(){
		$(".wan-oth").show(500);
		$(".wan-es").hide(500);
		$(".wan-ss").hide(500);
		$(".wan-ws").hide(500);
		$(".wan-bs").hide(500);
		$(".wan-yb").hide(500);
		$(".wan-sh").hide(500);
	});
	
	
	//第三組結束
	
	//全部==按鈕
			$(".qos").click(function(){
				$("*").show(500);
				$(".uio").show(500);
				$(".uip").show(500);
				$("#page").hide();
			});
	
			$(".zhan").click(function(){
				$("#page").slideToggle(500);
				
			});
	
			
	$(".hons").click(function(){
		$("*").toggleClass("red");
	});
	//變色(紅色)
	
	
	
	$(".newsli").first().css("color","#5A0305");
	$(".newsa").first().css("color","#5A0305");
	
	$(".newsli").eq(1).css("color","#C0832C");
	$(".newsa").eq(1).css("color","#C0832C");
	
	$(".newsli").eq(2).css("color","#12B4B7");
	$(".newsa").eq(2).css("color","#12B4B7");
	
	$(".newsli").eq(3).css("color","#000");
	$(".newsli").eq(4).css("color","#000");
	$(".newsli").eq(5).css("color","#000");
	$(".newsli").eq(6).css("color","#000");
	$(".newsli").eq(7).css("color","#000");
	$(".newsli").eq(8).css("color","#000");
	$(".newsli").eq(9).css("color","#000");
	
	
	//獲取外部文件代碼
	$(".huoq").click(function(){
		$("#div1").load("../DW+GY/css/loud.htm");
	});
	
	
	
	
});
❸ html簡單的分頁代碼怎麼寫
網頁鏈接
看一下這個吧,現在很少有人手動寫分頁了,一般都是用插件。或者現在主流的前端框架,都有用戶量特別大的前端組件庫,用起來很方便。其實這個分頁手寫js並不難,主要是理清邏輯就可以了,能寫但是沒必要~如果是比較老的前端框架,必須手寫js分頁邏輯,追問就行,我給你屢屢
