网站首页 > 技术文章 正文
直接上代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>分页功能</title>
<script src="js/jquery-1.11.0.js"></script>
<style>
*{margin:0;padding:0}
#paging{
width:100%;
height:auto;
margin-top:100px;
}
/* 内容样式 */
#paging .paging-content{
width:100%;
height:auto;
text-align: center;
}
#paging .paging-content .paging-content-text{
width:100%;
height:50px;
line-height:50px;
}
/*分页样式*/
#paging .paging-footer{
width:100%;
height:50px;
line-height:50px;
font-size:20px;
color:#fff;
background-color:#d3d3d3;
}
#paging .paging-footer .paging-footer-content{
text-align:center;
margin:auto;
}
#paging .paging-footer-content .content-text{
display:inline-block;
margin-right:15px;
}
#paging .paging-footer-content .content-text button{
color:#fff;
font-size:20px;
background-color: #d3d3d3;
outline: none;
border:none;
cursor:pointer;
}
#paging .paging-footer-content .content-text button:hover{
color:#2d8cf0;
}
#paging .paging-footer-content .content-text .upward{
cursor:no-drop;
}
#paging .paging-footer-content .content-text span{
color:#d3d3d3;
font-size:16px;
background-color:#fff;
border-radius:8px;
padding:1px 15px;
margin:0 10px;
}
#paging .paging-footer-content .content-text input{
width:30px;
font-size:16px;
background-color:#fff;
border-radius:8px;
padding:1px 15px;
margin:0 10px;
outline: none;
border:none;
}
</style>
</head>
<body>
<div id="paging">
<div class="paging-content">
</div>
<div class="paging-footer">
<div class="paging-footer-content">
<div class="content-text">
<button class="upward" disabled="disabled">上一页</button>
</div>
<div class="content-text">
第<span class="nowPage">1</span>页
</div>
<div class="content-text">
<button class="down">下一页</button>
</div>
<div class="content-text">
跳转<input class="pageVal" type="text" value="1" onKeyUp="value=value.replace(/\D/g,'')"/>页
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function(){
//页面加载初始化数据
var page = 1; //请求页数
var pageSize = 10; //每页返回数据条数
getData();
function getData(){
$.ajax({
type : 'GET',
url : 'datas.json',
data : {
page : page,
pageSize : pageSize
},
dataType: "json",
success: function(data){
//var res = data.data;
console.log(data);
$('#paging .paging-content').html(''); //每次请求清空原来的数据
$.each(res,function(index,res){
pagingContent =
'<div class="paging-content-text">'+res.bumenname+'</div>'
//将请求的数据append到页面上
$('#paging .paging-content').append(pagingContent);
})
}
});
}
//分页
var upwardPage = $(' #paging .content-text .upward'); //上一页
var downPage = $(' #paging .content-text .down'); //下一页
var nowPage = $(' #paging .content-text .nowPage'); //当前页数
var nowPageNum = Number(nowPage.html());
var pageVal = $(' #paging .content-text .pageVal'); //跳转页数
//上一页
upwardPage.click(function(){
nowPageNum = page;
if(nowPageNum > 1){
nowPageNum --;
}else{
nowPageNum == 1;
}
if(nowPageNum == 1){
upwardPage.attr('disabled',true);
upwardPage.css('cursor','no-drop');
nowPage.html(1);
pageVal.val(1);
page = nowPageNum;
getData();
}
else{
nowPage.html(nowPageNum);
pageVal.val(nowPageNum);
page = nowPageNum;
getData();
}
})
//下一页
downPage.click(function(){
nowPageNum = page;
upwardPage.attr('disabled',false);
upwardPage.css('cursor','pointer');
nowPageNum++;
nowPage.html(nowPageNum);
pageVal.val(nowPageNum);
page = nowPageNum;
getData();
})
//跳转
//失去焦点
pageVal.blur(function(){
var pageval = Number(pageVal.val());
var nowpage = Number(nowPage.html());
//如果失去焦点前后的值一样,不进行数据请求
if(pageval != nowpage){
//小于1跳转第一页
if(pageval > 1){
upwardPage.attr('disabled',false);
upwardPage.css('cursor','pointer');
nowPage.html(pageval);
page = pageval;
getData();
}else{
upwardPage.attr('disabled',true);
upwardPage.css('cursor','no-drop');
nowPage.html(1);
pageVal.val(1);
page = 1;
getData();
}
}
})
//回车
pageVal.keyup(function(event){
if(event.keyCode == 13){
var pageval = Number(pageVal.val());
var nowpage = Number(nowPage.html());
//如果失去焦点前后的值一样,不进行数据请求
if(pageval != nowpage){
//小于1跳转第一页
if(pageval > 1){
upwardPage.attr('disabled',false);
upwardPage.css('cursor','pointer');
nowPage.html(pageval);
page = pageval;
getData();
}else{
upwardPage.attr('disabled',true);
upwardPage.css('cursor','no-drop');
nowPage.html(1);
pageVal.val(1);
page = 1;
getData();
}
}
}
})
})
</script>
</body>
</html>
效果图:(内容倒是后换个地址就可以显示)
水平有限,欢迎指教。
猜你喜欢
- 2024-09-11 浅析MySQL Join Reorder算法(mysqlinner join)
- 2024-09-11 js 小函数(js函数总结)
- 2024-09-11 Kubernetes 高性能网络组件 Calico 入门教程
- 2024-09-11 jQuery中的clone妙用(jquery.on)
- 2024-09-11 自定义一个"骚气"的jQuery
- 2024-09-11 前端单元测试以及自动化构建入门(前端单元测试是什么)
- 2024-09-11 Python全栈 Web(jQuery 一条龙服务)
- 2024-09-11 jQuery遍历说、详解与示例的结合,轻松搞定这个遍历!
- 2024-09-11 「clickhouse专栏」对标mongodb存储类JSON数据文档统计分析
- 2024-09-11 jQuery实现简易购物车功能(jquery购物车结算页面)
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)