原理:全部數據已加載好,js經過遍歷部分顯示,實現分頁效果javascript
html代碼css
<html> <head> <meta charset='utf-8'> <script type="text/javascript" src="page.js"></script> <style type="text/css"> #idData {color: red;border: solid;text-align: center;} a{text-decoration: none;} </style> </head> <body onLoad="goPage(1,10);"> <table id="idData" width="70%"> <tr><td>liujinzhong1</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong2</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong3</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong4</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong5</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong6</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong7</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong8</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong9</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong10</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong11</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong12</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong13</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong14</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong15</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong16</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong17</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong18</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong19</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong20</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong21</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong22</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong23</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong24</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong25</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong26</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong27</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong28</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong29</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong30</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong31</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong32</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong33</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong34</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong35</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong36</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong37</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong38</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong39</td><td>25</td><td>男</td><td>山西呂梁</td></tr> <tr><td>liujinzhong40</td><td>25</td><td>男</td><td>山西呂梁</td></tr> </table> <table width="60%" align="right"> <tr><td><div id="barcon" name="barcon"></div></td></tr> </table> </body> </html>
js實現分頁html
/** * 分頁函數 * pno--頁數 * psize--每頁顯示記錄數 * 分頁部分是從真實數據行開始,於是存在加減某個常數,以肯定真正的記錄數 * 純js分頁實質是數據行所有加載,經過是否顯示屬性完成分頁功能 **/ function goPage(pno,psize){ var itable = document.getElementById("idData"); var num = itable.rows.length;//表格全部行數(全部記錄數) console.log(num); var totalPage = 0;//總頁數 var pageSize = psize;//每頁顯示行數 //總共分幾頁 if(num/pageSize > parseInt(num/pageSize)){ totalPage=parseInt(num/pageSize)+1; }else{ totalPage=parseInt(num/pageSize); } var currentPage = pno;//當前頁數 var startRow = (currentPage - 1) * pageSize+1;//開始顯示的行 31 var endRow = currentPage * pageSize;//結束顯示的行 40 endRow = (endRow > num)? num : endRow; 40 console.log(endRow); //遍歷顯示數據實現分頁 for(var i=1;i<(num+1);i++){ var irow = itable.rows[i-1]; if(i>=startRow && i<=endRow){ irow.style.display = "block"; }else{ irow.style.display = "none"; } } var pageEnd = document.getElementById("pageEnd"); var tempStr = "共"+num+"條記錄 分"+totalPage+"頁 當前第"+currentPage+"頁"; if(currentPage>1){ tempStr += "<a href=\"#\" onClick=\"goPage("+(1)+","+psize+")\">首頁</a>"; tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage-1)+","+psize+")\"><上一頁</a>" }else{ tempStr += "首頁"; tempStr += "<上一頁"; } if(currentPage<totalPage){ tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage+1)+","+psize+")\">下一頁></a>"; tempStr += "<a href=\"#\" onClick=\"goPage("+(totalPage)+","+psize+")\">尾頁</a>"; }else{ tempStr += "下一頁>"; tempStr += "尾頁"; } document.getElementById("barcon").innerHTML = tempStr; }
效果java