美文网首页
原生JS 实现选项卡功能

原生JS 实现选项卡功能

作者: 飞鱼_JS | 来源:发表于2017-08-24 22:41 被阅读0次
    <!DOCTYPE html>
    <html>
     <head>
     <meta charset="UTF-8">
     <title>原生js选项卡写法</title>
     <style>
      #div1 div{width: 200px;height: 200px;border: 1px solid #FF0000;display: none;}
      .active{background: red;}
     </style>
     <script>
      window.onload=function(){//原生js选项卡写法
      var oDiv = document.getElementById('div1');
      var aInput = document.getElementsByTagName('input');
      var aCon = oDiv.getElementsByTagName('div');
      for(var i=0;i<aInput.length;i++){
       aInput[i].index = i;
       aInput[i].onclick = function(){
       for(var i=0;i<aInput.length;i++){
        aInput[i].className = '';
        aCon[i].style.display = 'none';
       }
       this.className= 'active';
       aCon[this.index].style.display = 'block';
       }
      }
      }
     </script>
     </head>
     <body>
     <div id="div1">
      <input class="active" type="button" value="1" />
      <input type="button" value="2"/>
      <input type="button" value="3"/>
      <div style="display: block;">11111111111</div>
      <div>22222222222</div>
      <div>333333333333</div>
     </div>
     </body>
    </html>
    

    相关文章

      网友评论

          本文标题:原生JS 实现选项卡功能

          本文链接:https://www.haomeiwen.com/subject/mfmzkxtx.html