js 点击在同一页切换不同的内容

<!doctype html>
<!--只做一个模板,样式就不做了-->
< html  lang = 'zh-cn' >
< head >
     < meta  http-equiv = "Content-Type"  content = "text/html; charset=UTF-8" >
     < style >
         *{margin: 0;padding: 0;}
         #container{
             height: 180px;
             width: 240px;
             margin: 200px auto;
         }
         #btn{
             float: left;
             width: 60px;
             height: 180px;
         }
         #tab{
             float: right;
             width: 178px;
             height: 178px;
             margin-left: -180px;
             border: 1px solid black;
             position: relative;
         }
         .tab_btn{
             display: block;
             width: 58px;
             height: 58px;
             background: green;
             line-height: 60px;
             text-align: center;
             border: 1px solid red;
             text-decoration: none;
             color: black;
         }
 
         .tab_btn:hover{
             background: blue;
             color: white;
         }
 
         .tab_div{
             position: absolute;
             left: 0px;
             top: 0px;
             display: none;
         }
 
         .curr_btn{
             background: blue !important;
             color: white;
         }
         .curr_div{
             display: block !important;
         }
     </ style >
     < script >
       function getClass(className) { //className指class的值
       
                 var tagname = document.getElementsByTagName('*');  //获取指定元素
                 var tagnameAll = [];     //这个数组用于存储所有符合条件的元素
                 for (var i = 0; i < tagname.length; i++) {     //遍历获得的元素
                     if (tagname[i].className.indexOf(className)>=0){     //如果获得的元素中的class的值等于指定的类名,就赋值给tagnameAll
                         tagnameAll[tagnameAll.length] = tagname[i];
                     }
                 }
                 return tagnameAll;
             
         }
 
 
 
         window.onload=function(){//载入事件
             var btn=getClass('tab_btn');//获取按钮数组
             var div=getClass('tab_div');//获取div数组
             for(i=0;i< btn.length ;i++){
                 btn[i] .onclick = function (){//按钮点击事件
                     var index=(this.getAttribute('index')-0);//获取是第几个按钮按下
                     if(btn[index].className.indexOf('curr_btn')>=0) return;//如果按下的按钮为当前选中的按钮则无反应
                     for(i=0;i< btn.length ;i++){
                         if(index==i){
                             btn[i] .className = 'tab_btn curr_btn' ;
                             div[i] .className = 'tab_div curr_div' ;
                         }else{
                             btn[i] .className = 'tab_btn' ;
                             div[i] .className = 'tab_div' ;
                         }
                     }
                 }
             }
         };
     </script>
</ head >
 
 
< body >
 
     < div  id = "container" >
         < div  id = 'btn' >
             < a  href = 'javascript:void(0)'  index = '0'  class = 'tab_btn curr_btn' >1</ a >
             < a  href = 'javascript:void(0)'  index = '1'  class = 'tab_btn' >2</ a >
             < a  href = 'javascript:void(0)'  index = '2'  class = 'tab_btn' >3</ a >
         </ div >
         < div  id = 'tab' >
             < div  class = 'tab_div curr_div' >
                 div1
             </ div >
             < div  class = 'tab_div' >
                 div2
             </ div >
             < div  class = 'tab_div' >
                 div3
             </ div >
         </ div >
     </ div >
 
</ body >
 
</ html >

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值