选项卡案例

由于未提供博客具体内容,无法给出包含关键信息的摘要。
<!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>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    } 

    .box {
      width: 500px;
      height: 320px;
      display: flex;
      flex-direction: column;
      margin: 50px auto;
      border: 3px solid #333;
    }

    .box>ul {
      height: 60px;
      display: flex;
    }

    .box>ul>li {
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 30px;
      color: #fff;
      background-color: skyblue;
      cursor: pointer;
    }

    .box>ul>li.active {
      background-color: orange;
    }

    .box>ol {
      flex: 1;
      position: relative;
    }

    .box>ol>li {
      width: 100%;
      height: 100%;
      background-color: purple;
      font-size: 50px;
      color: #fff;
      position: absolute;
      left: 0;
      top: 0;
      display: none;
    }

    .box>ol>li.active {
      display: block;
    }
  </style>
</head>

<body>

  <div class="box" id="box1">
    <ul>
      <li>1</li>
      <li>2</li> 
      <li>3</li>
    </ul>
    <ol> 
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ol>
  </div>

  <script>
    // 实现思路:记录当前选中项的索引,先根据索引删除对应的选中项样式;再给点击的选项设置样式

    // 1. 获取页面元素
    let box1 = document.querySelector('#box1')
    let btns = box1.querySelectorAll('ul li')
    let conts = box1.querySelectorAll('ol li')

    let index = 0; // 这个索引就是每一个需要删除样式的索引

    // 打开页面时,默认选中第一项
    btns[index].classList.add('active')
    conts[index].classList.add('active')

    // 2. 循环绑定点击事件
    for (let i = 0; i < btns.length; i++) {
      btns[i].onclick = function () {
        // 清掉上一个选中样式
        btns[index].classList.remove('active')
        conts[index].classList.remove('active')

        // 记录下来点击的按钮的索引,并赋值给index
        index = i

        // 给点击的按钮设置选中样式
        btns[index].classList.add('active')
        conts[index].classList.add('active')
      }
    }
  </script>

</body>

</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值