a标签实现当前页面跳转显示相应div,以及js实现上下页查看

<!DOCTYPE html>
<html>
<head>
  <style>
      *{
          margin: 0;
          padding: 0;
      }
      .box{
          width: 100%;
          height: 100%;
      }
      ul{
        width: 100%;
        height: 50px;
        line-height: 50px;
        list-style: none;
        position: fixed;
        top: 0px;
        z-index: 1000;
        /* background: white; */
      }
      ul>a>li{
          float: left;
          width: 23%;
          text-align: center;
          /* border: 1px solid black; */
      }

    .content{
        position: fixed;
        top: 50px;
        bottom: 50px;
        overflow: auto;
        width: 100%;
    }
    .content div{
        float: left;
        width: 100%;
        height: 100%;
        text-align: center;
        color: white;
        line-height: 500px;
        
    }
    .foot{
        height: 50px;
        position: fixed;
        bottom: 0px;
        width: 100%;
    }
  </style>
</head>
<body>
<div class="box">
        <header>
            <ul>
                <a href="#a"><li >1111</li></a>
                <a href="#b"><li>2222</li></a>
                <a href="#c"><li>3333</li></a>
                <a href="#d"><li>4444</li></a>
            </ul>
        </header>
        <div class="content">
        <div id="a" style="background: blue;"><h1>11111</h1></div>
        <div id="b" style="background: blueviolet;"><h1>22222</h1></div>
        <div id="c" style="background: cadetblue;"><h1>33333</h1></div>
        <div id="d" style="background: crimson;"><h1>44444</h1></div>
        </div>
        <div class="foot">
            <button onclick="prev()">上一页</button>
            <button onclick="next()">下一页</button>
        </div>
</div>
</body>

<script>
        const Alist = document.querySelectorAll('a>li')
        Alist[0].style.background='red';
        let index = 0;
        for(let i in Alist){
            Alist[i].addEventListener('click',function(){
                this.style.background='red';
                index =  parseInt(i)
                for(var a =0; a <= Alist.length-1;a++){
                    if(i != a){
                        Alist[a].style.background='white';
                    }   
                }
            })

        };
        function prev(){
            if(index > 0){
                index -= 1;
                Alist[index].style.background='red';
                console.log(Alist[index].parentNode)
                Alist[index].parentNode.setAttribute("onclick",'');
                Alist[index].parentNode.click()
                for(var a =0; a <= Alist.length-1;a++){
                    if(index != a){
                        Alist[a].style.background='white';
                    }   
                }
            }
        };

        function next(){
            if(index < Alist.length-1){
                 index += 1;
                Alist[index].style.background='red';
                Alist[index].parentNode.setAttribute("onclick",'');
                Alist[index].parentNode.click()
                for(var a =0; a <= Alist.length-1;a++){
                    if(index != a){
                        Alist[a].style.background='white';
                    }   
                };
            }

        }
</script>
</html>      

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值