jq实现新闻列表上一页下一页及详情页

一.html

     <ul class="new-list">
  
  </ul>
  <div class="btns">
    <button class="prev">上一页</button>
    <button class="next">下一页</button>
  </div>

二.css

 *{
  padding:0;
  margin:0;
  list-style:none;
}
a{
  text-decoration: none;
}
.clearfix::after{
  content: "";
  clear: both;
  display: block;
  width:0;
  height: 0;
}
.new-list,.btns{
  width:500px;
  margin:50px auto;
}
.new-list li{
  border-bottom: 1px solid gainsboro;
  padding:20px 0;
}
.new-list li:last-child{
  border-bottom:none;
}
.new-list li img{
  float: left;
  width: 100px;
  margin-right: 10px;
} 
.news-dec p{
  color:gainsboro;
}
i{
  color: red;
}

三.script

      var pageNums=1;
getList(pageNums)
// 点击下一页
$(".next").click(function(){
    pageNums++
  getList(pageNums)
})

// 点击上一页
$(".prev").click(function(){
  if(pageNums===1){
    alert("已经是首页了")
  }
    pageNums--
  getList(pageNums)
})
// 函数封装
function getList(pageNums){ 
$.ajax({
  url:"https://api.apiopen.top/getJoke",
  method:"POST",
  data:{
    page:pageNums,
    count:5,
  },
  success:function(res){
    console.log(res);
    if(res.code===200){
      $(".new-list").html("")
      for(var i=0;i<res.result.length;i++){
        var item=res.result[i]
        var str='<li class="clearfix">'+
      '<img src="'+item.thumbnail+'" alt="断网了">'+
      '<div class="news-dec">'+
       ' <a target="_blank"  href="./详情.html?id='+item.sid+'"><h3>'+item.text +'</h3></a>'+
       ' <p>'+item.passtime+'</p>'+
       '<i>类型:'+item.type +'</i>'+
     ' </div>'+
   ' </li>'
    $(".new-list").append(str)
      }
    }else{
      alert(res.massage)
    }
  }
})
}        
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值