1源碼css
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>test</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"> <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script> </head> <body> <table class="table table-hover" id="pageShow"> <tr> <th>name</th> <th>age</th> <th>phone</th> </tr> </table> <nav aria-label="Page navigation"> <ul class="pagination" id="page"> <li class="prePage"> <a href="#" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> </ul> </nav> <script> $(function(){ var data = [ //模擬的假數據 {name:'1',age:12,phone:10086}, {name:'2',age:12,phone:10086}, {name:'3',age:12,phone:10086}, {name:'4',age:12,phone:10086}, {name:'5',age:12,phone:10086}, {name:'6',age:12,phone:10086}, {name:'7',age:12,phone:10086}, {name:'8',age:12,phone:10086}, {name:'9',age:12,phone:10086}, {name:'10',age:12,phone:10086}, {name:'11',age:12,phone:10086}, {name:'12',age:12,phone:10086}, {name:'13',age:12,phone:10086}, {name:'14',age:12,phone:10086}, {name:'15',age:12,phone:10086}, {name:'16',age:12,phone:10086}, {name:'17',age:12,phone:10086}, {name:'18',age:12,phone:10086}, {name:'19',age:12,phone:10086}, ]; var pageSize = 5;//每頁的條數 var totalPage = Math.ceil(data.length/pageSize);//總頁數 var currentPage;//當前頁數 var startData;//開始的數據 var endData;//結尾的數 for(let i=1;i<=totalPage;i++){ $('#page').append('<li class="page-item"><a href="#">'+i+'</a></li>'); } $('#page').append( `<li class="nextPage"> <a href="#" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li>`); function pageination(i){ currentPage = i; console.log('第'+currentPage+'頁...'); startData = (currentPage-1)*pageSize; endData = currentPage*pageSize - 1; if(endData>data.length){ endData = data.length; } $('.tr-item').remove();//移除列表顯示的數據 for(let i=startData;i<endData;i++){//添加列表顯示的數據 $('#pageShow').append('<tr class="tr-item">'+ '<td>'+data[i].name+'</td>'+ '<td>'+data[i].age+'</td>'+ '<td>'+data[i].phone+'</td>'+ '</tr>') } } //初始化 $('.page-item').eq(0).addClass('active'); pageination(1); //對每一個頁數的點擊事件的綁定 $('.page-item').on('click',function(){ let index = $(this).index();//獲取點擊的頁數 $(this).addClass('active').siblings().removeClass('active'); pageination(index); }) //向前按鈕事件綁定 $('.prePage').click(function(){ $('.page-item').find(function(){ let index = $('.active').index() - 1; if(index <1){ index = 1; } $('.page-item').eq(index-1).addClass('active').siblings().removeClass('active'); pageination(index); }) }) //向後按鈕事件綁定 $('.nextPage').click(function(){ $('.page-item').find(function(){ let index = $('.active').index() + 1; if(index >totalPage){ index = totalPage; } $('.page-item').eq(index-1).addClass('active').siblings().removeClass('active'); pageination(index); }) }) }) </script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script> </body> </html>
思路:html
利用事件綁定來使頁面顯示咱們要顯示的jquery