原生tab选项卡

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      span {
        display: inline-block;
        width: 100px;
        height: 30px;
        background-color: #ccc;
        text-align: center;
        line-height: 30px;
        cursor: pointer;
      }

      .active {
        background: red;
        color: white;
      }

      div div {
        width: 350px;
        height: 200px;
        border: 1px solid #ccc;
        font-size: 40px;
        line-height: 200px;
        text-align: center;
        display: none;
      }

      div div.show {
        display: block;
      }
    </style>
    <script>
      var box = null;
      var span = null;
      var div = null;
      window.onload = function () {
        box = document.getElementById("box");
      
     
      var aaa =new Tab('box').init()
    
      };
      function Tab(id) {
         this.box= document.getElementById(id);
         this.span = box.getElementsByTagName('span');
         this.div  = box.getElementsByTagName('div');
      }

      Tab.prototype.init = function () {
        var _this = this;
        for (var i = 0; i < this.span.length; i++) {
         
          this.span[i].index = i;
          this.span[i].onclick = function () {
            _this.change(this);
          };
        }
      };
      Tab.prototype.change = function (obj) {
        for (var i = 0; i < this.span.length; i++) {
          this.span[i].className = "";
          this.div[i].className = "";
        }
        obj.className = "";
        this.div[obj.index].className = "show";
        this.span[obj.index].className ='active'
      };
    </script>
  </head>

  <body>
    <div id="box">
      <span class="active">html</span>
      <span>css</span>
      <span>js</span>
      <div class="show">html</div>
      <div>css</div>
      <div>js</div>
    </div>
  </body>
</html>
posted @ 2021-01-04 15:42  诡道也  阅读(74)  评论(0编辑  收藏  举报