原生js选项卡

本文介绍了一个使用CSS进行样式设计及JavaScript实现元素显示控制的简单网页案例。通过CSS选择器为不同列表项设置背景颜色,并利用JavaScript监听点击事件切换另一组元素的显示状态。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <title>Title</title>
  <style>
    *{
      margin:0;
      padding:0;
    }
    ul li{
      list-style: none;
    }
    .ul1{
      display: flex;
    }
    .ul1 li{
      width: 25%;
      height: 50px;
    }
    .ul1 li:nth-child(2n){
      background: orange;
    }
    .ul1 li:nth-child(2n+1){
      background: blue;
    }
    .ul2 li:not(:first-child){
      display: none;
    }
    .ul2 li:nth-child(2n){
      background: red;
      width: 100%;
      height:500px;
    }
    .ul2 li:nth-child(2n+1){
      background: gray;
      width: 100%;
      height:500px;
    }
  </style>
</head>
<body>
<ul class="ul1" id="u1">
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
<ul class="ul2" id="u2">
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
</body>
</html>
<script>
  window.onload = function(){
        var lis = document.getElementById("u1").children;
        var u2s = document.getElementById("u2").children;
        for(let i=0;i<lis.length;i++){
          lis[i].onclick = function(){
            this.index = i;
            for(let i=0;i<u2s.length;i++){
              u2s[i].style.display = "none";
            }
            u2s[this.index].style.display = "block";
          }
        }


  }
</script>

 

转载于:https://www.cnblogs.com/dujunfeng/p/7901181.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值