这篇文章主要介绍jquery如何优化分页操作,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创建数据库语句
CREATE TABLE `news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8
记录查询页面
test.php
<?php
header("Content-Type:text/html;Charset=utf-8");
$mysqli = new mysqli("localhost","root","","test");
$mysqli->set_charset("utf8");
$query = $mysqli->query("SELECT * FROM news");
$res = $query->fetch_all(MYSQLI_ASSOC);
echo json_encode($res);
分页显示页面
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
.page{
width: 1000px;
margin-left: auto;
margin-right: auto;
text-align:center;
display:none;
}
</style>
</head>
<body>
<div id="listTag">
</div>
</body>
<script type="text/javascript" src="http://localhost/jquery/jquery.js"></script>
<script type="text/javascript">
$(function(){
var avgNum = 18; //每页显示18条记录
$.ajax({
type:"GET",
url:"http://localhost/test.php",
dataType:"json",
success:function(data){
var totalPage = data.length; //获取总的记录数
var pageNum = Math.ceil(totalPage/avgNum); //计算得到页数
var content = '';
for(var i=1;i<=pageNum;i++){
content += '<div id="page'+i+'" class="page"><ul>';
for(var j=((i-1)*avgNum);j<(i*avgNum)&&j<totalPage;j++){
content += '<li>'+data[j].title+'</li>';
}
content += '</ul>';
content += '<hr/><div>';
//前一页
if(i == 1){
content += '<<  ';
}else{
content += '<a href="javascript:showPage('+(i-1)+')" rel="external nofollow" rel="external nofollow" ><<</a>  ';
}
//后一页
if(i == pageNum){
content += '>>  ';
}else{
content += '<a href="javascript:showPage('+(i+1)+')" rel="external nofollow" rel="external nofollow" >>></a>  ';
}
content += '</div></div>';
}
//生成所有的div,并隐藏
$("#listTag").append(content);
showPage(1);
}
});
});
function showPage(num){
$("#page"+num).show();
$("#page"+num).siblings().hide();
}
</script>
</html>
分页效果:
分页样式二
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
.page{
width: 1000px;
margin-left: auto;
margin-right: auto;
text-align:center;
display:none;
}
.active{
color:red;
}
</style>
</head>
<body>
<div id="listTag">
</div>
</body>
<script type="text/javascript" src="http://localhost/jquery/jquery.js"></script>
<script type="text/javascript">
$(function(){
var avgNum = 18; //每页显示18条记录
$.ajax({
type:"GET",
url:"http://localhost/test.php",
dataType:"json",
success:function(data){
var totalPage = data.length; //获取总的记录数
var pageNum = Math.ceil(totalPage/avgNum); //计算得到页数
var content = '';
for(var i=1;i<=pageNum;i++){
content += '<div id="page'+i+'" class="page"><ul>';
for(var j=((i-1)*avgNum);j<(i*avgNum)&&j<totalPage;j++){
content += '<li>'+data[j].title+'</li>';
}
content += '</ul>';
content += '<hr/><div>';
//前一页
if(i == 1){
content += '<<  ';
}else{
content += '<a href="javascript:showPage('+(i-1)+')" rel="external nofollow" rel="external nofollow" ><<</a>  ';
}
//显示所有的页码
for(var p=1;p<=pageNum;p++){
content += '<a href="javascript:showPage('+p+')" rel="external nofollow" class="p'+p+'">'+p+'</a>  ';
}
//后一页
if(i == pageNum){
content += '>>  ';
}else{
content += '<a href="javascript:showPage('+(i+1)+')" rel="external nofollow" rel="external nofollow" >>></a>  ';
}
content += '</div></div>';
}
//生成所有的div,并隐藏
$("#listTag").append(content);
showPage(1);
}
});
});
function showPage(num){
$("#page"+num).show();
$("#page"+num).siblings().hide();
$(".p"+num).addClass("active");
$(".p"+num).siblings().removeClass("active");
}
</script>
</html>
显示效果
以上是“jquery如何优化分页操作”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!