zoukankan      html  css  js  c++  java
  • JS实现选项卡示例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <!--思路
    1. 获取元素;
    2. for循环按钮元素添加onclick(点击) 或者 onmousemove(移入)事件;
    3. 点击当前按钮时会以高亮状态显示,通过for循环历遍把所有的按钮class样式设置为''空 then 把所有div的display设置为none。
    4. 点击当前按钮添加class样式'active',把当前div显示出来,display设置为block。-->
    
    <style>
    .active{background:#3CC;}
    .div2{width:300px;height:200px; border:1px #666666 solid;display:none;}
    </style>
    <script>
    window.onload=function(){
       var odiv=document.getElementById('div1');//获取div
       var btn=odiv.getElementsByTagName('input');//获取div下的input
       var div2=odiv.getElementsByTagName('div') ;//获取div下的div
     
       for(i=0;i<btn.length;i++)//循环每个按钮
       { 
         btn[i].index=i; //btn[i]是指定button的第i个按钮;为该按钮添加一个index属性,并将index的值设置为i
         btn[i].onclick=function()//按钮的第i个点击事件
         {
             for(i=0;i<btn.length;i++)//循环去掉button的样式,把div隐藏
             { 
               btn[i].className=''; //清空按钮的样式
               div2[i].style.display='none';//隐藏div
             }
             this.className='active';//this指调用该点击事件的input按钮添加className为active
             div2[this.index].style.display='block';//this.index是当前div 
         };
       }
    };
    </script>
    </head>
    
    <body>
    <div id="div1">
        <input type="button" class="active" value="1"/>
        <input type="button" value="2"/>
        <input type="button" value="3"/> 
        <input type="button" value="4"/>
          <div class="div2" style="display:block;">11</div>
          <div class="div2">22</div>
          <div class="div2">33</div>
          <div class="div2">44</div>
    </div>
    </body>
    </html>
  • 相关阅读:
    mysql字符生命周期
    mysql5.6特殊字符问题
    电信网关-天翼网关-GPON-HS8145C设置桥接路由拨号认证
    linux-shell脚本高并发对文本url批量下载
    Kettle7.1在window启动报错
    微软的在线文档存储OneDrive使用帮助
    centos6.5搭建redmine3.4
    mysql基础拓扑图
    线上应用故障排查之一:高CPU占用
    线上服务CPU100%问题快速定位实战
  • 原文地址:https://www.cnblogs.com/blacop/p/5911762.html
Copyright © 2011-2022 走看看