var html=$('#temp').html();
var curPage=0,pages=0;
$.getJSON('php/pages.php',function (res) {
pages=Math.ceil(res/4);/*获取信息的总页数*/
});
function show(cPage){//替换每一页的内容
$.getJSON('php/listmore.php',{cPage:cPage},function (json) {
var str='';
$('#list').empty();
json.forEach(function (el) {
str+=html.replace('{id}',el.id).replace('{title}',el.title).replace('{src}',el.src)
.replace('{content}',el.content).replace('{date}',el.time);
});
$('#list').html(str);
});
$('#total').html((curPage+1)+'/'+pages);
}
setTimeout(function () {
show(0);
},100);
$('#page').on('click','li',function () {//上下翻页,翻遍当前页的值
var i=$(this).data('i');//jquery里特有的获取data-*属性的方法
switch (i){
case 0:curPage=0;break;
case 1:curPage>0?curPage--:0;break;
case 2:curPage<(pages-1)?curPage++:pages-1;break;
case 3:curPage=pages-1;break;
}
show(curPage);
disabled(curPage);
})
function disabled(curPage) {//关于临界值禁止选择
if (curPage==0){/*当前页为第一页,首页和上一页选项禁止点击*/
$(&#39;#index&#39;).addClass(&#39;disabled&#39;).next().addClass(&#39;disabled&#39;);
$(&#39;#end&#39;).removeClass(&#39;disabled&#39;).prev().removeClass(&#39;disabled&#39;);
} else if (curPage==pages-1){
$(&#39;#index&#39;).removeClass(&#39;disabled&#39;).next().removeClass(&#39;disabled&#39;);
$(&#39;#end&#39;).addClass(&#39;disabled&#39;).prev().addClass(&#39;disabled&#39;);
} else {/*当前页为最后一页,尾页和下一页选项禁止点击*/
$(&#39;#index&#39;).removeClass(&#39;disabled&#39;).next().removeClass(&#39;disabled&#39;);
$(&#39;#end&#39;).removeClass(&#39;disabled&#39;).prev().removeClass(&#39;disabled&#39;);
}
}
connect.php(连接数据库)
query("set names utf8");
pages.php(获取总页数)
query("select * from news");
$row=$result->rowCount();
echo $row;
listmore.php(获取数据库里的数据)
query("select * from news order by id desc limit {$start},$num");
$link = null;
while ($row=$result->fetch()){/*每一次读取一条数据*/
$json[]=$row;/*把数据赋给json数组*/
}
echo json_encode($json);/*把json数组以json格式返回给HTML*/