实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Tabs</title> <style> *{ margin: 0px; padding: 0px; border-collapse: collapse; } li{ /* 清除li的点 */ list-style:none; /* 左浮动,横向排列 */ float: left; width: 50px; height: 30px; /* 水平对齐 */ text-align: center; /* 居中对齐 和 height同高 */ line-height: 30px; border: 1px solid #ccc; background :#cca; /* 鼠标小手 */ cursor: pointer; } /* 清除浮动 */ .clearFix{ clear:both; } /* 设置内容Tab */ .contents{ width: 153px; height : 150px; border: 1px solid #ccc; background: #cca; } .outcontents{ width: 153px; height: 150px; /* 溢出隐藏,为了只显示第一张 */ overflow: hidden; } </style> </head> <body> <div id='tabs'> <ul> <li><a onmouseover="fun_tab(0)">TabA</a></li> <li><a onmouseover="fun_tab(1)">TabB</a></li> <li><a onmouseover="fun_tab(2)">TabC</a></li> </ul> <div class="clearFix"></div> <div class="outcontents"> <div class='contents'>TAB-A-Contents</div> <div class='contents'>TAB-B-Contents</div> <div class='contents'>TAB-C-Contents</div> <div class='contents'>TAB-D-Contents</div> </div> </div> <script> function fun_tab(num){ // 获取所有tab内容的dom var contents = document.getElementsByClassName('contents'); // 设置内容dom的CSS: display:none ,隐藏 for (let i = 0; i < contents.length; i++) { contents[i].style.display ="none"; } // 点那个就显示哪个 contents[num].style.display ='block'; } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例