经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
jQuery-tab切换
来源:cnblogs  作者:Tynam.Yang  时间:2019/6/17 9:00:04  对本文有异议
  1. 1 <!DOCTYPE html>
  2. 2 <html lang="en">
  3. 3 <head>
  4. 4 <meta charset="UTF-8">
  5. 5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. 6 <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. 7 <title>Tab</title>
  8. 8 <style>
  9. 9 * {
  10. 10 margin: 0;
  11. 11 padding: 0
  12. 12 }
  13. 13
  14. 14 ul {
  15. 15 list-style: none;
  16. 16 }
  17. 17
  18. 18 #contain {
  19. 19 margin: 10px auto;
  20. 20 width: 300px;
  21. 21 height: 500px;
  22. 22 border: solid rgb(224, 222, 222);
  23. 23 border-radius: 10px;
  24. 24 overflow: scroll;
  25. 25 }
  26. 26
  27. 27 #contain h4 {
  28. 28 margin-top: 5px;
  29. 29 text-align: center;
  30. 30 height: 30px;
  31. 31 line-height: 30px;
  32. 32 border-bottom: solid rgb(240, 240, 240);
  33. 33 }
  34. 34
  35. 35 /* tab */
  36. 36 #coupon>li {
  37. 37 float: left;
  38. 38 margin-top: 10px;
  39. 39 display: inline-block;
  40. 40 width: 100px;
  41. 41 border: solid rgb(240, 240, 240);
  42. 42 height: 30px;
  43. 43 line-height: 30px;
  44. 44 font-size: 12px;
  45. 45 color: rgb(177, 175, 175);
  46. 46 box-sizing:border-box;
  47. 47 text-align: center;
  48. 48 }
  49. 49
  50. 50 #coupon>li:first-child {
  51. 51 border-left: 0;
  52. 52 border-right: 0;
  53. 53 }
  54. 54
  55. 55 #coupon>li:last-child {
  56. 56 border-left: 0;
  57. 57 border-right: 0;
  58. 58 }
  59. 59
  60. 60 #coupon .active {
  61. 61 border: solid red;
  62. 62 background: red;
  63. 63 color: #fff;
  64. 64 }
  65. 65
  66. 66 /* 优惠券内容 */
  67. 67 #coupon-contain {
  68. 68 margin: 50px 5px;
  69. 69 }
  70. 70
  71. 71 #coupon-contain>div>div {
  72. 72 margin: 10px 5px;
  73. 73 text-align: center;
  74. 74 height: 50px;
  75. 75 line-height: 50px;
  76. 76 border-radius: 8px;
  77. 77 background: rebeccapurple;
  78. 78 }
  79. 79
  80. 80 /* 隐藏所有优惠券 */
  81. 81 #coupon-contain>div {
  82. 82 display: none;
  83. 83 }
  84. 84
  85. 85 /* 显示class为show的元素 */
  86. 86 #coupon-contain>.show {
  87. 87 display: block;
  88. 88 }
  89. 89
  90. 90 #coupon-contain>div:first-child>div {
  91. 91 background: green;
  92. 92 }
  93. 93
  94. 94 #coupon-contain>div:last-child>div {
  95. 95 background: #333;
  96. 96 }
  97. 97 </style>
  98. 98
  99. 99 <script src="jquery-3.4.1.js"></script>
  100. 100 <script>
  101. 101 $(function() {
  102. 102 var $coupon_tab = $("#coupon>li");
  103. 103 var $coupon_contain = $('#coupon-contain>div');
  104. 104 var index = 0;
  105. 105
  106. 106 $coupon_tab.click(function(){
  107. 107 //移除tab中class的active属性
  108. 108 $($coupon_tab[index]).removeClass('active');
  109. 109 //移除优惠券中class的active属性
  110. 110 $($coupon_contain[index]).removeClass('show');
  111. 111
  112. 112 index = $(this).index();
  113. 113 //添加tab中class的active属性
  114. 114 $(this).addClass('active');
  115. 115 //添加优惠券中class的active属性
  116. 116 $($coupon_contain[index]).addClass('show');
  117. 117
  118. 118 });
  119. 119 });
  120. 120 </script>
  121. 121 </head>
  122. 122 <body>
  123. 123 <div id="contain">
  124. 124 <h4>优惠券</h4>
  125. 125 <ul id="coupon">
  126. 126 <li class="active">未使用</li>
  127. 127 <li>已使用</li>
  128. 128 <li>已过期</li>
  129. 129 </ul>
  130. 130 <div id="coupon-contain">
  131. 131 <div id=“coupon-unused class="show">
  132. 132 <div>未使用的优惠券</div>
  133. 133 <div>未使用的优惠券</div>
  134. 134 <div>未使用的优惠券</div>
  135. 135 </div>
  136. 136 <div id=“coupon-used class="hide">
  137. 137 <div>已使用的优惠券</div>
  138. 138 <div>已使用的优惠券</div>
  139. 139 <div>已使用的优惠券</div>
  140. 140 <div>已使用的优惠券</div>
  141. 141 </div>
  142. 142 <div id=“coupon-expired class="hide">
  143. 143 <div>已过期的优惠券</div>
  144. 144 <div>已过期的优惠券</div>
  145. 145 <div>已过期的优惠券</div>
  146. 146 </div>
  147. 147 </div>
  148. 148 </div>
  149. 149 </body>
  150. 150 </html>

效果图

原文链接:http://www.cnblogs.com/tynam/p/11029670.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号