当前位置:   article > 正文

js Tab导航栏切换_tab-content

tab-content

 

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <style type="text/css">
  7. *{
  8. margin: 0;
  9. padding: 0;
  10. box-sizing: border-box;
  11. }
  12. .box{
  13. margin: 100px auto;
  14. width: 500px;
  15. }
  16. .tab-list {
  17. height: 30px;
  18. line-height: 30px;
  19. }
  20. ul li {
  21. list-style: none;
  22. float: left;
  23. width: 20%;
  24. padding: 0 40px;
  25. border: 1px solid gray;
  26. cursor: pointer;
  27. }
  28. .current {
  29. background-color: pink;
  30. }
  31. .tab-content {
  32. height: 300px;
  33. border: 1px solid gray;
  34. border-top: 0;
  35. }
  36. .tab-content > p{
  37. height: 100%;
  38. text-align: center;
  39. display: none;
  40. }
  41. p.show {
  42. display: block;
  43. background-color: pink;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <div class="box">
  49. <div class="tab-list">
  50. <ul>
  51. <li class="current">1</li>
  52. <li>2</li>
  53. <li>3</li>
  54. <li>4</li>
  55. <li>5</li>
  56. </ul>
  57. </div>
  58. <div class="tab-content">
  59. <p class="show">1</p>
  60. <p>2</p>
  61. <p>3</p>
  62. <p>4</p>
  63. <p>5</p>
  64. </div>
  65. </div>
  66. </body>
  67. <script type="text/javascript">
  68. //获取所有li
  69. var lis = document.querySelectorAll("li");
  70. //获取所有内容
  71. var ps = document.querySelectorAll("p");
  72. for (var i = 0; i < lis.length; i++) {
  73. //给每个li设置一个index属性值
  74. lis[i].index = i;
  75. lis[i].onmousemove = function(){
  76. for (var j = 0; j < lis.length; j++) {
  77. //以添加class类名的方式控制内容的隐藏与显示
  78. //排他思维 先清空所有类名
  79. lis[j].className="";
  80. ps[j].className="";
  81. }
  82. //给当前移入的元素添加类名
  83. this.className="current";
  84. ps[this.index].className="show";
  85. }
  86. }
  87. </script>
  88. </html>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号