test.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>tab切换</title>
  6. <style type="text/css">
  7. #box{
  8. height: 400px;
  9. width: 600px;
  10. border: 1px solid #ccc;
  11. padding: 10px;
  12. }
  13. ul{
  14. height: 30px;
  15. width: 600px;
  16. padding: 0;
  17. margin: 0;
  18. }
  19. li{
  20. display: block;
  21. text-align: center;
  22. width: 100px;
  23. float: left;
  24. list-style: none;
  25. cursor: pointer;
  26. height: 30px;
  27. line-height: 30px;
  28. }
  29. .choice{
  30. background: #409EFF;
  31. color: #fff;
  32. }
  33. .tab-item{
  34. display: none;
  35. }
  36. .show{
  37. display: block;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <ul>
  43. <li tabid="1" class="choice">tab1</li>
  44. <li tabid="2">tab2</li>
  45. <li tabid="3">tab3</li>
  46. </ul>
  47. <div id="box">
  48. <div class="tab-item show">Tab-item1</div>
  49. <div class="tab-item">Tab-item2</div>
  50. <div class="tab-item">Tab-item3</div>
  51. </div>
  52. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  53. <script type="text/javascript">
  54. $("li").click(function(){
  55. // 修改tab标签样式
  56. $(this).attr("class","choice")
  57. $(this).siblings().attr("class","")
  58. // 获取tab ID
  59. var itemId = $(this).attr("tabid")-1;
  60. // 切换到指定tab
  61. $("#box").find("div:eq("+itemId+")").attr("class","show")
  62. $("#box").find("div:eq("+itemId+")").siblings().attr("class","tab-item")
  63. })
  64. </script>
  65. </body>
  66. </html>