Tab selection in bootstrap nav-tab

huangapple go评论90阅读模式
英文:

Tab selection in bootstrap nav-tab

问题

I am using WordPress and want to use nav-tab in the Bootstrap library.

The default nav-tab mode of the Bootstrap library is very simple, so I want to improve it with a little script code.

I want the first tab to be active when the page is loaded and its background to be different from the rest of the tabs. But when another tab is clicked, the new tab will be activated and its background will change, and the first tab will take the default background and become inactive.

I wrote a separate script for each tab, which is very bad, and worse, this code does not work at all. That is, by clicking on each tab instead of the background (li); The background (ul) changes.

英文:

I am using wordpress and want to use nav-tab in bootstrap library.

The default nav-tab mode of the Bootstrap library is very simple, so I want to improve it with a little script code.

I want the first tab to be active when the page is loaded and its background to be different from the rest of the tabs. But when another tab is clicked, the new tab will be activated and its background will change, and the first tab will take the default background and become inactive.

I wrote a separate script for each tab, which is very bad, and worse, this code does not work at all. That is, by clicking on each tab instead of the background (li); The background (ul) changes.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. $(&#39;.nav_main&#39;).find(&#39;li.content-desc&#39;).click(function () {
  2. if ($(this).parent().find(&quot;ul&quot;).eq(0).is(&#39;:visible&#39;)) {
  3. $(this).parent().removeClass(&#39;active&#39;);
  4. } else {
  5. $(this).parent().addClass(&#39;active&#39;);
  6. }
  7. });
  8. $(&#39;.nav_main&#39;).find(&#39;li.content-author&#39;).click(function () {
  9. if ($(this).parent().find(&quot;ul&quot;).eq(0).is(&#39;:visible&#39;)) {
  10. $(this).parent().removeClass(&#39;active&#39;);
  11. } else {
  12. $(this).parent().addClass(&#39;active&#39;);
  13. }
  14. });
  15. $(&#39;.nav_main&#39;).find(&#39;li.content-comment&#39;).click(function () {
  16. if ($(this).parent().find(&quot;ul&quot;).eq(0).is(&#39;:visible&#39;)) {
  17. $(this).parent().removeClass(&#39;active&#39;);
  18. } else {
  19. $(this).parent().addClass(&#39;active&#39;);
  20. }
  21. });
  22. $(&#39;.nav_main&#39;).find(&#39;li.content-tag&#39;).click(function () {
  23. if ($(this).parent().find(&quot;ul&quot;).eq(0).is(&#39;:visible&#39;)) {
  24. $(this).parent().removeClass(&#39;active&#39;);
  25. } else {
  26. $(this).parent().addClass(&#39;active&#39;);
  27. }
  28. });

<!-- language: lang-css -->

  1. .content_main {
  2. width: 650px;
  3. border: 1px solid #00000033;
  4. text-align: justify;
  5. margin: 0px 5px 10px 5px;
  6. padding: 20px;
  7. border-radius: 5px;
  8. line-height: 35px;
  9. }
  10. .nav_main ul.active {
  11. background: #b421f3;
  12. }
  13. .content-tab {
  14. border: none;
  15. }
  16. .nav-tabs&gt;li&gt;a {
  17. background: #e2e2e2;
  18. border: 1px solid #00000033;
  19. box-shadow: 0 -2px 0 #2196f3;
  20. padding: 14px;
  21. color: #000;
  22. margin: 0px 15px 0px 5px;
  23. border-radius: 5px 5px 0 0;
  24. }
  25. .content-tab {
  26. margin-top: 20px;
  27. }
  28. .content-tab a:hover {
  29. text-decoration: none;
  30. }
  31. .content-comment {
  32. margin-right: -10px;
  33. }

<!-- language: lang-html -->

  1. &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
  2. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  3. &lt;div class=&quot;container&quot;&gt;
  4. &lt;div class=&quot;nav_main&quot;&gt;
  5. &lt;!-- Nav tabs --&gt;
  6. &lt;ul class=&quot;nav nav-tabs content-tab&quot; role=&quot;tablist&quot;&gt;
  7. &lt;li role=&quot;presentation&quot; class=&quot;content-desc&quot;&gt;&lt;a class=&quot;desc&quot; href=&quot;#desc&quot; aria-controls=&quot;desc&quot;
  8. role=&quot;tab&quot; data-toggle=&quot;tab&quot;&gt;content&lt;/a&gt;&lt;/li&gt;
  9. &lt;li role=&quot;presentation&quot; class=&quot;content-author&quot;&gt;&lt;a class=&quot;author&quot; href=&quot;#author&quot; aria-controls=&quot;author&quot;
  10. role=&quot;tab&quot; data-toggle=&quot;tab&quot;&gt;author&lt;/a&gt;&lt;/li&gt;
  11. &lt;li role=&quot;presentation&quot; class=&quot;content-comment&quot;&gt;&lt;a class=&quot;comment&quot; href=&quot;#comment&quot;
  12. aria-controls=&quot;comment&quot; role=&quot;tab&quot; data-toggle=&quot;tab&quot;&gt;comment&lt;/a&gt;&lt;/li&gt;
  13. &lt;li role=&quot;presentation&quot; class=&quot;content-tag&quot;&gt;&lt;a class=&quot;tag&quot; href=&quot;#tag&quot; aria-controls=&quot;tag&quot; role=&quot;tab&quot;
  14. data-toggle=&quot;tab&quot;&gt;tag&lt;/a&gt;&lt;/li&gt;
  15. &lt;/ul&gt;
  16. &lt;!-- Tab panes --&gt;
  17. &lt;div class=&quot;tab-content content_main&quot;&gt;
  18. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane active&quot; id=&quot;desc&quot;&gt;
  19. desc desc desc desc desc desc desc
  20. &lt;/div&gt;
  21. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;author&quot;&gt;
  22. author author author author author
  23. &lt;/div&gt;
  24. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;comment&quot;&gt;
  25. comment comment comment comment comment comment
  26. &lt;/div&gt;
  27. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;tag&quot;&gt;
  28. tag tag tag tag tag tag
  29. &lt;/div&gt;
  30. &lt;/div&gt;
  31. &lt;/div&gt;
  32. &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

以下是代码部分的翻译:

Demo Code :

  1. $("#myTab li > a").click(function(e) {
  2. e.preventDefault();
  3. $("#myTab li > a ,.tab-pane").removeClass("active");
  4. $(this).addClass("active");
  5. $(".tab-pane").hide(); // 隐藏其他选项卡
  6. $(".tab-content " + $(this).attr("href")).addClass("active").show(); // 显示当前选项卡
  7. });
  8. $('#myTab > li:eq(0) a').click(); // 触发点击第一个选项卡
  1. .content_main {
  2. width: 650px;
  3. border: 1px solid #00000033;
  4. text-align: justify;
  5. margin: 0px 5px 10px 5px;
  6. padding: 20px;
  7. border-radius: 5px;
  8. line-height: 35px;
  9. }
  10. .nav_main .active {
  11. background: #b421f3;
  12. }
  13. .content-tab {
  14. border: none;
  15. }
  16. .nav-tabs>li>a {
  17. background: #e2e2e2;
  18. border: 1px solid #00000033;
  19. box-shadow: 0 -2px 0 #2196f3;
  20. padding: 14px;
  21. color: #000;
  22. margin: 0px 15px 0px 5px;
  23. border-radius: 5px 5px 0 0;
  24. }
  25. .content-tab {
  26. margin-top: 20px;
  27. }
  28. .content-tab a:hover {
  29. text-decoration: none;
  30. }
  31. .content-comment {
  32. margin-right: -10px;
  33. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
  3. <div class="container">
  4. <div class="nav_main">
  5. <!-- 导航选项卡 -->
  6. <ul class="nav nav-tabs content-tab" role="tablist" id="myTab">
  7. <li role="presentation" class="content-desc"><a class="desc active" href="#desc" aria-controls="desc" role="tab" data-toggle="tab">内容</a></li>
  8. <li role="presentation" class="content-author"><a class="author" href="#author" aria-controls="author" role="tab" data-toggle="tab">作者</a></li>
  9. <li role="presentation" class="content-comment"><a class="comment" href="#comment" aria-controls="comment" role="tab" data-toggle="tab">评论</a></li>
  10. <li role="presentation" class="content-tag"><a class="tag" href="#tag" aria-controls="tag" role="tab" data-toggle="tab">标签</a></li>
  11. </ul>
  12. <!-- 选项卡内容 -->
  13. <div class="tab-content content_main">
  14. <div role="tabpanel" class="tab-pane active" id="desc">
  15. 描述 描述 描述 描述 描述 描述 描述
  16. </div>
  17. <div role="tabpanel" class="tab-pane" id="author">
  18. 作者 作者 作者 作者 作者
  19. </div>
  20. <div role="tabpanel" class="tab-pane" id="comment">
  21. 评论 评论 评论 评论 评论 评论
  22. </div>
  23. <div role="tabpanel" class="tab-pane" id="tag">
  24. 标签 标签 标签 标签 标签
  25. </div>
  26. </div>
  27. </div>
  28. </div>

另一种方法是:

Demo Code :

  1. const triggerTabList = document.querySelectorAll('#myTab a');
  2. triggerTabList.forEach(triggerEl => {
  3. const tabTrigger = new bootstrap.Tab(triggerEl);
  4. triggerEl.addEventListener('click', event => {
  5. event.preventDefault();
  6. tabTrigger.show();
  7. });
  8. });
  9. document.querySelector('#myTab > li:first-child a').click();
  1. .content_main {
  2. width: 650px;
  3. border: 1px solid #00000033;
  4. text-align: justify;
  5. margin: 0px 5px 10px 5px;
  6. padding: 20px;
  7. border-radius: 5px;
  8. line-height: 35px;
  9. }
  10. .nav_main .active {
  11. background: #b421f3;
  12. }
  13. .content-tab {
  14. border: none;
  15. }
  16. .nav-tabs>li>a {
  17. background: #e2e2e2;
  18. border: 1px solid #00000033;
  19. box-shadow: 0 -2px 0 #2196f3;
  20. padding: 14px;
  21. color: #000;
  22. margin: 0px 15px 0px 5px;
  23. border-radius: 5px 5px 0 0;
  24. }
  25. .content-tab {
  26. margin-top: 20px;
  27. }
  28. .content-tab a:hover {
  29. text-decoration: none;
  30. }
  31. .content-comment {
  32. margin-right: -10px;
  33. }
  1. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
  2. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous">
  3. <div class="container">
  4. <div class="nav_main">
  5. <!-- 导航选项卡 -->
  6. <ul class="nav nav-tabs content-tab" role="tablist" id="myTab">
  7. <li role="presentation" class="content-desc"><a class="desc" href="#desc" aria-controls="desc" role="tab" data-toggle="tab">内容</a></li>
  8. <li role="presentation" class="content-author"><a class="author" href="#author" aria-controls="author" role="tab" data-toggle="tab">作者</a></li>
  9. <li role="presentation" class="content-comment"><a class="comment" href="#comment" aria-controls="comment" role="tab" data-toggle="tab">评论</a></li>
  10. <li role="presentation" class="content-tag"><a class="tag" href="#tag" aria-controls="tag" role="tab"
  11. <details>
  12. <summary>英文:</summary>
  13. You can keep only one click event for all tabs and inside that using `$(this)` and `id` show or hide your tabs.
  14. ***Demo Code :***
  15. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  16. &lt;!-- language: lang-js --&gt;
  17. $(&quot;#myTab li &gt; a &quot;).click(function(e) {
  18. e.preventDefault();
  19. $(&quot;#myTab li &gt; a ,.tab-pane&quot;).removeClass(&quot;active&quot;);
  20. $(this).addClass(&quot;active&quot;)
  21. $(&quot;.tab-pane&quot;).hide() //hide all other tabs
  22. $(&quot;.tab-content &quot; + $(this).attr(&quot;href&quot;)).addClass(&quot;active&quot;).show(); //show curent
  23. })
  24. $(&#39;#myTab &gt; li:eq(0) a&#39;).click() // trigger click on first a
  25. &lt;!-- language: lang-css --&gt;
  26. .content_main {
  27. width: 650px;
  28. border: 1px solid #00000033;
  29. text-align: justify;
  30. margin: 0px 5px 10px 5px;
  31. padding: 20px;
  32. border-radius: 5px;
  33. line-height: 35px;
  34. }
  35. .nav_main .active {
  36. background: #b421f3;
  37. }
  38. .content-tab {
  39. border: none;
  40. }
  41. .nav-tabs&gt;li&gt;a {
  42. background: #e2e2e2;
  43. border: 1px solid #00000033;
  44. box-shadow: 0 -2px 0 #2196f3;
  45. padding: 14px;
  46. color: #000;
  47. margin: 0px 15px 0px 5px;
  48. border-radius: 5px 5px 0 0;
  49. }
  50. .content-tab {
  51. margin-top: 20px;
  52. }
  53. .content-tab a:hover {
  54. text-decoration: none;
  55. }
  56. .content-comment {
  57. margin-right: -10px;
  58. }
  59. &lt;!-- language: lang-html --&gt;
  60. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  61. &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD&quot; crossorigin=&quot;anonymous&quot;&gt;
  62. &lt;div class=&quot;container&quot;&gt;
  63. &lt;div class=&quot;nav_main&quot;&gt;
  64. &lt;!-- Nav tabs --&gt;
  65. &lt;ul class=&quot;nav nav-tabs content-tab&quot; role=&quot;tablist&quot; id=&quot;myTab&quot;&gt;
  66. &lt;li role=&quot;presentation&quot; class=&quot;content-desc&quot;&gt;&lt;a class=&quot;desc active&quot; href=&quot;#desc&quot; aria-controls=&quot;desc&quot; role=&quot;tab&quot; data-toggle=&quot;tab&quot;&gt;content&lt;/a&gt;&lt;/li&gt;
  67. &lt;li role=&quot;presentation&quot; class=&quot;content-author&quot;&gt;&lt;a class=&quot;author&quot; href=&quot;#author&quot; aria-controls=&quot;author&quot; role=&quot;tab&quot; data-toggle=&quot;tab&quot;&gt;author&lt;/a&gt;&lt;/li&gt;
  68. &lt;li role=&quot;presentation&quot; class=&quot;content-comment&quot;&gt;&lt;a class=&quot;comment&quot; href=&quot;#comment&quot; aria-controls=&quot;comment&quot; role=&quot;tab&quot; data-toggle=&quot;tab&quot;&gt;comment&lt;/a&gt;&lt;/li&gt;
  69. &lt;li role=&quot;presentation&quot; class=&quot;content-tag&quot;&gt;&lt;a class=&quot;tag&quot; href=&quot;#tag&quot; aria-controls=&quot;tag&quot; role=&quot;tab&quot; data-toggle=&quot;tab&quot;&gt;tag&lt;/a&gt;&lt;/li&gt;
  70. &lt;/ul&gt;
  71. &lt;!-- Tab panes --&gt;
  72. &lt;div class=&quot;tab-content content_main&quot;&gt;
  73. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane active&quot; id=&quot;desc&quot;&gt;
  74. desc desc desc desc desc desc desc
  75. &lt;/div&gt;
  76. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;author&quot;&gt;
  77. author author author author author
  78. &lt;/div&gt;
  79. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;comment&quot;&gt;
  80. comment comment comment comment comment comment
  81. &lt;/div&gt;
  82. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;tag&quot;&gt;
  83. tag tag tag tag tag tag
  84. &lt;/div&gt;
  85. &lt;/div&gt;
  86. &lt;/div&gt;
  87. &lt;/div&gt;
  88. &lt;!-- end snippet --&gt;
  89. Other way : You can enable each tab via click event and initialise your tabs using `bootstrap.Tab()` and can make the first tab active by calling `.click()` on it .
  90. ***Demo Code*** :
  91. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  92. &lt;!-- language: lang-js --&gt;
  93. const triggerTabList = document.querySelectorAll(&#39;#myTab a&#39;)
  94. triggerTabList.forEach(triggerEl =&gt; {
  95. const tabTrigger = new bootstrap.Tab(triggerEl)
  96. triggerEl.addEventListener(&#39;click&#39;, event =&gt; {
  97. event.preventDefault()
  98. tabTrigger.show()
  99. })
  100. })
  101. document.querySelector(&#39;#myTab &gt; li:first-child a&#39;).click()
  102. &lt;!-- language: lang-css --&gt;
  103. .content_main {
  104. width: 650px;
  105. border: 1px solid #00000033;
  106. text-align: justify;
  107. margin: 0px 5px 10px 5px;
  108. padding: 20px;
  109. border-radius: 5px;
  110. line-height: 35px;
  111. }
  112. .nav_main .active {
  113. background: #b421f3;
  114. }
  115. .content-tab {
  116. border: none;
  117. }
  118. .nav-tabs&gt;li&gt;a {
  119. background: #e2e2e2;
  120. border: 1px solid #00000033;
  121. box-shadow: 0 -2px 0 #2196f3;
  122. padding: 14px;
  123. color: #000;
  124. margin: 0px 15px 0px 5px;
  125. border-radius: 5px 5px 0 0;
  126. }
  127. .content-tab {
  128. margin-top: 20px;
  129. }
  130. .content-tab a:hover {
  131. text-decoration: none;
  132. }
  133. .content-comment {
  134. margin-right: -10px;
  135. }
  136. &lt;!-- language: lang-html --&gt;
  137. &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD&quot; crossorigin=&quot;anonymous&quot;&gt;
  138. &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  139. &lt;div class=&quot;container&quot;&gt;
  140. &lt;div class=&quot;nav_main&quot;&gt;
  141. &lt;!-- Nav tabs --&gt;
  142. &lt;ul class=&quot;nav nav-tabs content-tab&quot; role=&quot;tablist&quot; id=&quot;myTab&quot;&gt;
  143. &lt;li role=&quot;presentation&quot; class=&quot;content-desc&quot;&gt;&lt;a class=&quot;desc&quot; href=&quot;#desc&quot; aria-controls=&quot;desc&quot; role=&quot;tab&quot; data-toggle=&quot;tab&quot;&gt;content&lt;/a&gt;&lt;/li&gt;
  144. &lt;li role=&quot;presentation&quot; class=&quot;content-author&quot;&gt;&lt;a class=&quot;author&quot; href=&quot;#author&quot; aria-controls=&quot;author&quot; role=&quot;tab&quot; data-toggle=&quot;tab&quot;&gt;author&lt;/a&gt;&lt;/li&gt;
  145. &lt;li role=&quot;presentation&quot; class=&quot;content-comment&quot;&gt;&lt;a class=&quot;comment&quot; href=&quot;#comment&quot; aria-controls=&quot;comment&quot; role=&quot;tab&quot; data-toggle=&quot;tab&quot;&gt;comment&lt;/a&gt;&lt;/li&gt;
  146. &lt;li role=&quot;presentation&quot; class=&quot;content-tag&quot;&gt;&lt;a class=&quot;tag&quot; href=&quot;#tag&quot; aria-controls=&quot;tag&quot; role=&quot;tab&quot; data-toggle=&quot;tab&quot;&gt;tag&lt;/a&gt;&lt;/li&gt;
  147. &lt;/ul&gt;
  148. &lt;!-- Tab panes --&gt;
  149. &lt;div class=&quot;tab-content content_main&quot;&gt;
  150. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane active&quot; id=&quot;desc&quot;&gt;
  151. desc desc desc desc desc desc desc
  152. &lt;/div&gt;
  153. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;author&quot;&gt;
  154. author author author author author
  155. &lt;/div&gt;
  156. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;comment&quot;&gt;
  157. comment comment comment comment comment comment
  158. &lt;/div&gt;
  159. &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;tag&quot;&gt;
  160. tag tag tag tag tag tag
  161. &lt;/div&gt;
  162. &lt;/div&gt;
  163. &lt;/div&gt;
  164. &lt;/div&gt;
  165. &lt;!-- end snippet --&gt;
  166. </details>
  167. # 答案2
  168. **得分**: 1
  169. 你可以在不使用jQuery的情况下制作这个以下是翻译后的部分
  170. ```html
  171. <!-- 开始片段:js 隐藏: false 控制台: true Babel: false -->
  172. <!-- 语言: lang-css -->
  173. .tab-content {
  174. max-width: 650px;
  175. border: 1px solid #00000033;
  176. text-align: justify;
  177. margin: 0px 5px 10px 5px;
  178. padding: 20px;
  179. border-radius: 5px;
  180. line-height: 35px;
  181. }
  182. .nav-link.active {
  183. background-color: #b421f3 !important;
  184. }
  185. .nav-tabs {
  186. border: none;
  187. }
  188. .nav-tabs > li > .nav-link {
  189. background: #e2e2e2;
  190. border: 1px solid #00000033;
  191. box-shadow: 0 -2px 0 #2196f3;
  192. padding: 14px;
  193. color: #000;
  194. margin: 0px 15px 0px 5px;
  195. border-radius: 5px 5px 0 0;
  196. }
  197. .nav-tabs {
  198. margin-top: 20px;
  199. }
  200. .nav-link:hover {
  201. text-decoration: none;
  202. }
  203. <!-- 语言: lang-html -->
  204. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"/>
  205. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
  206. <div class="container">
  207. <div class="nav_main">
  208. <!-- 导航标签 -->
  209. <ul class="nav nav-tabs" role="tablist">
  210. <li class="nav-item" role="presentation">
  211. <button class="nav-link active" id="content-tab" data-bs-toggle="tab" data-bs-target="#content" type="button" role="tab" aria-controls="content" aria-selected="true">content</button>
  212. </li>
  213. <li class="nav-item" role="presentation">
  214. <button class="nav-link" id="author-tab" data-bs-toggle="tab" data-bs-target="#author" type="button" role="tab" aria-controls="author" aria-selected="false">author</button>
  215. </li>
  216. <li class="nav-item" role="presentation">
  217. <button class="nav-link" id="comment-tab" data-bs-toggle="tab" data-bs-target="#comment" type="button" role="tab" aria-controls="comment" aria-selected="false">comment</button>
  218. </li>
  219. <li class="nav-item" role="presentation">
  220. <button class="nav-link" id="tag-tab" data-bs-toggle="tab" data-bs-target="#tag" type="button" role="tab" aria-controls="tag" aria-selected="false">tag</button>
  221. </li>
  222. </ul>
  223. <!-- 标签面板 -->
  224. <div class="tab-content">
  225. <div class="tab-pane fade show active" id="content" role="tabpanel" aria-labelledby="content-tab" tabindex="0">
  226. desc desc desc desc desc desc desc
  227. </div>
  228. <div class="tab-pane fade" id="author" role="tabpanel" aria-labelledby="author-tab" tabindex="0">
  229. author author author author author
  230. </div>
  231. <div class="tab-pane fade" id="comment" role="tabpanel" aria-labelledby="comment-tab" tabindex="0">
  232. comment comment comment comment comment comment
  233. </div>
  234. <div class="tab-pane fade" id="tag" role="tabpanel" aria-labelledby="tag-tab" tabindex="0">
  235. tag tag tag tag tag tag
  236. </div>
  237. </div>
  238. </div>
  239. </div>
  240. <!-- 结束片段 -->
英文:

you can make thit without Jquery

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. .tab-content {
  2. max-width: 650px;
  3. border: 1px solid #00000033;
  4. text-align: justify;
  5. margin: 0px 5px 10px 5px;
  6. padding: 20px;
  7. border-radius: 5px;
  8. line-height: 35px;
  9. }
  10. .nav-link.active {
  11. background-color: #b421f3 !important;
  12. }
  13. .nav-tabs {
  14. border: none;
  15. }
  16. .nav-tabs &gt; li &gt; .nav-link {
  17. background: #e2e2e2;
  18. border: 1px solid #00000033;
  19. box-shadow: 0 -2px 0 #2196f3;
  20. padding: 14px;
  21. color: #000;
  22. margin: 0px 15px 0px 5px;
  23. border-radius: 5px 5px 0 0;
  24. }
  25. .nav-tabs {
  26. margin-top: 20px;
  27. }
  28. .nav-link:hover {
  29. text-decoration: none;
  30. }

<!-- language: lang-html -->

  1. &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
  2. &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js
  3. &quot;&gt;&lt;/script&gt;
  4. &lt;div class=&quot;container&quot;&gt;
  5. &lt;div class=&quot;nav_main&quot;&gt;
  6. &lt;!-- Nav tabs --&gt;
  7. &lt;ul class=&quot;nav nav-tabs&quot; role=&quot;tablist&quot;&gt;
  8. &lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;
  9. &lt;button class=&quot;nav-link active&quot; id=&quot;content-tab&quot; data-bs-toggle=&quot;tab&quot; data-bs-target=&quot;#content&quot; type=&quot;button&quot; role=&quot;tab&quot; aria-controls=&quot;content&quot; aria-selected=&quot;true&quot;&gt;content&lt;/button&gt;
  10. &lt;/li&gt;
  11. &lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;
  12. &lt;button class=&quot;nav-link&quot; id=&quot;author-tab&quot; data-bs-toggle=&quot;tab&quot; data-bs-target=&quot;#author&quot; type=&quot;button&quot; role=&quot;tab&quot; aria-controls=&quot;author&quot; aria-selected=&quot;false&quot;&gt;author&lt;/button&gt;
  13. &lt;/li&gt;
  14. &lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;
  15. &lt;button class=&quot;nav-link&quot; id=&quot;comment-tab&quot; data-bs-toggle=&quot;tab&quot; data-bs-target=&quot;#comment&quot; type=&quot;button&quot; role=&quot;tab&quot; aria-controls=&quot;comment&quot; aria-selected=&quot;false&quot;&gt;comment&lt;/button&gt;
  16. &lt;/li&gt;
  17. &lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;
  18. &lt;button class=&quot;nav-link&quot; id=&quot;tag-tab&quot; data-bs-toggle=&quot;tab&quot; data-bs-target=&quot;#tag&quot; type=&quot;button&quot; role=&quot;tab&quot; aria-controls=&quot;tag&quot; aria-selected=&quot;false&quot;&gt;tag&lt;/button&gt;
  19. &lt;/li&gt;
  20. &lt;/ul&gt;
  21. &lt;!-- Tab panes --&gt;
  22. &lt;div class=&quot;tab-content&quot;&gt;
  23. &lt;div class=&quot;tab-pane fade show active&quot; id=&quot;content&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;content-tab&quot; tabindex=&quot;0&quot;&gt;
  24. desc desc desc desc desc desc desc
  25. &lt;/div&gt;
  26. &lt;div class=&quot;tab-pane fade&quot; id=&quot;author&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;author-tab&quot; tabindex=&quot;0&quot;&gt;
  27. author author author author author
  28. &lt;/div&gt;
  29. &lt;div class=&quot;tab-pane fade&quot; id=&quot;comment&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;comment-tab&quot; tabindex=&quot;0&quot;&gt;
  30. comment comment comment comment comment comment
  31. &lt;/div&gt;
  32. &lt;div class=&quot;tab-pane fade&quot; id=&quot;tag&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;tag-tab&quot; tabindex=&quot;0&quot;&gt;
  33. tag tag tag tag tag tag
  34. &lt;/div&gt;
  35. &lt;/div&gt;
  36. &lt;/div&gt;
  37. &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月21日 00:52:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定