点击(此处)折叠或打开
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>JavaScript tab 效果</title>
- <style type="text/css">
- *{
- margin:0;
- padding:0;
- }
- button{
- width: 298px;
- height: 80px;
- margin-left: -10px;
- font-size: 30px;
- }
- #button{
- width: 1000px;
- margin-left: 28px;
-
- }
- #tablist{
- width: 886px;
- margin-left: 20px;
- height: 500px;
- outline:1px solid red;
- font-size:50px;
- }
- .list{
- display:none;
- }
- .tab1{
- background: black;
- color: white;
-
- }
- .tab2{
- background: #FF0000;
- }
-
- </style>
- </head>
- <body>
- <div id="button">
- <button class="tab1">tab1</button>
- <button>tab2</button>
- <button>tab3</button>
- </div>
- <div id="tablist">
- <div class="list" style="display: block;">内容一</div>
- <div class="list">内容二</div>
- <div class="list">内容三</div>
- </div>
- <script type="text/javascript">
- //获取 按键button
- var Button=document.getElementsByTagName("button");
- // console.log(Button);
- //获取 id
- Tablist = document.getElementById("tablist");
- // console.log(Tablist);
- //获取 按键
- var Div=Tablist.getElementsByTagName("div");
- // console.log(Div);
- //第一层循环
- for(var b=0; b<Button.length;b++){
- //点几事件
- Button[b].onclick=function(){
-
- //第二层循环
- for( var a=0; a<Div.length;a++){
- //进行判断如果两个数字的索引相同,就会显示出来相应button取tab1的样式
- //否则,就会被隐藏 样式为空
- if(this == Button[a]){
-
- Div[a].style.display="block";
- this.className="tab1";
-
-
- }else{
- Div[a].style.display="none";
- // Button[a].setAttribute("class","");
- Button[a].className='';
- }
- }
- }
- }
- </script>
- </body>
- </html>