Tab selection in bootstrap nav-tab

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

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 -->

       $(&#39;.nav_main&#39;).find(&#39;li.content-desc&#39;).click(function () {
            if ($(this).parent().find(&quot;ul&quot;).eq(0).is(&#39;:visible&#39;)) {
                $(this).parent().removeClass(&#39;active&#39;);
            } else {
                $(this).parent().addClass(&#39;active&#39;);
            }
        });

        $(&#39;.nav_main&#39;).find(&#39;li.content-author&#39;).click(function () {
            if ($(this).parent().find(&quot;ul&quot;).eq(0).is(&#39;:visible&#39;)) {
                $(this).parent().removeClass(&#39;active&#39;);
            } else {
                $(this).parent().addClass(&#39;active&#39;);
            }
        });

        $(&#39;.nav_main&#39;).find(&#39;li.content-comment&#39;).click(function () {
            if ($(this).parent().find(&quot;ul&quot;).eq(0).is(&#39;:visible&#39;)) {
                $(this).parent().removeClass(&#39;active&#39;);
            } else {
                $(this).parent().addClass(&#39;active&#39;);
            }
        });

        $(&#39;.nav_main&#39;).find(&#39;li.content-tag&#39;).click(function () {
            if ($(this).parent().find(&quot;ul&quot;).eq(0).is(&#39;:visible&#39;)) {
                $(this).parent().removeClass(&#39;active&#39;);
            } else {
                $(this).parent().addClass(&#39;active&#39;);
            }
        });

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

.content_main {
    width: 650px;
    border: 1px solid #00000033;
    text-align: justify;
    margin: 0px 5px 10px 5px;
    padding: 20px;
    border-radius: 5px;
    line-height: 35px;
}

.nav_main ul.active {
    background: #b421f3;
}

.content-tab {
    border: none;
}

.nav-tabs&gt;li&gt;a {
    background: #e2e2e2;
    border: 1px solid #00000033;
    box-shadow: 0 -2px 0 #2196f3;
    padding: 14px;
    color: #000;
    margin: 0px 15px 0px 5px;
    border-radius: 5px 5px 0 0;
}

.content-tab {
    margin-top: 20px;
}

.content-tab a:hover {
    text-decoration: none;
}

.content-comment {
    margin-right: -10px;
}

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

&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;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;



&lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;nav_main&quot;&gt;
            &lt;!-- Nav tabs --&gt;
            &lt;ul class=&quot;nav nav-tabs content-tab&quot; role=&quot;tablist&quot;&gt;
                &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;
                &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;
                &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;
                &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;
            &lt;/ul&gt;
            &lt;!-- Tab panes --&gt;
            &lt;div class=&quot;tab-content content_main&quot;&gt;
                &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane active&quot; id=&quot;desc&quot;&gt;
                    desc desc desc desc desc desc desc
                &lt;/div&gt;
                &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;author&quot;&gt;
                    author author author author author
                &lt;/div&gt;
                &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;comment&quot;&gt;
                    comment comment comment comment comment comment
                &lt;/div&gt;
                &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;tag&quot;&gt;
                    tag tag tag tag tag tag
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;

    &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

以下是代码部分的翻译:

Demo Code :

$("#myTab li > a").click(function(e) {
  e.preventDefault();
  $("#myTab li > a ,.tab-pane").removeClass("active");
  $(this).addClass("active");
  $(".tab-pane").hide(); // 隐藏其他选项卡
  $(".tab-content " + $(this).attr("href")).addClass("active").show(); // 显示当前选项卡
});

$('#myTab > li:eq(0) a').click(); // 触发点击第一个选项卡
.content_main {
  width: 650px;
  border: 1px solid #00000033;
  text-align: justify;
  margin: 0px 5px 10px 5px;
  padding: 20px;
  border-radius: 5px;
  line-height: 35px;
}

.nav_main .active {
  background: #b421f3;
}

.content-tab {
  border: none;
}

.nav-tabs>li>a {
  background: #e2e2e2;
  border: 1px solid #00000033;
  box-shadow: 0 -2px 0 #2196f3;
  padding: 14px;
  color: #000;
  margin: 0px 15px 0px 5px;
  border-radius: 5px 5px 0 0;
}

.content-tab {
  margin-top: 20px;
}

.content-tab a:hover {
  text-decoration: none;
}

.content-comment {
  margin-right: -10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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">

<div class="container">
  <div class="nav_main">
    <!-- 导航选项卡 -->
    <ul class="nav nav-tabs content-tab" role="tablist" id="myTab">
      <li role="presentation" class="content-desc"><a class="desc active" href="#desc" aria-controls="desc" role="tab" data-toggle="tab">内容</a></li>
      <li role="presentation" class="content-author"><a class="author" href="#author" aria-controls="author" role="tab" data-toggle="tab">作者</a></li>
      <li role="presentation" class="content-comment"><a class="comment" href="#comment" aria-controls="comment" role="tab" data-toggle="tab">评论</a></li>
      <li role="presentation" class="content-tag"><a class="tag" href="#tag" aria-controls="tag" role="tab" data-toggle="tab">标签</a></li>
    </ul>
    <!-- 选项卡内容 -->
    <div class="tab-content content_main">
      <div role="tabpanel" class="tab-pane active" id="desc">
        描述 描述 描述 描述 描述 描述 描述
      </div>
      <div role="tabpanel" class="tab-pane" id="author">
        作者 作者 作者 作者 作者
      </div>
      <div role="tabpanel" class="tab-pane" id="comment">
        评论 评论 评论 评论 评论 评论
      </div>
      <div role="tabpanel" class="tab-pane" id="tag">
        标签 标签 标签 标签 标签
      </div>
    </div>
  </div>
</div>

另一种方法是:

Demo Code :

const triggerTabList = document.querySelectorAll('#myTab a');
triggerTabList.forEach(triggerEl => {
  const tabTrigger = new bootstrap.Tab(triggerEl);

  triggerEl.addEventListener('click', event => {
    event.preventDefault();
    tabTrigger.show();
  });
});

document.querySelector('#myTab > li:first-child a').click();
.content_main {
  width: 650px;
  border: 1px solid #00000033;
  text-align: justify;
  margin: 0px 5px 10px 5px;
  padding: 20px;
  border-radius: 5px;
  line-height: 35px;
}

.nav_main .active {
  background: #b421f3;
}

.content-tab {
  border: none;
}

.nav-tabs>li>a {
  background: #e2e2e2;
  border: 1px solid #00000033;
  box-shadow: 0 -2px 0 #2196f3;
  padding: 14px;
  color: #000;
  margin: 0px 15px 0px 5px;
  border-radius: 5px 5px 0 0;
}

.content-tab {
  margin-top: 20px;
}

.content-tab a:hover {
  text-decoration: none;
}

.content-comment {
  margin-right: -10px;
}
<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">
<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">

<div class="container">
  <div class="nav_main">
    <!-- 导航选项卡 -->
    <ul class="nav nav-tabs content-tab" role="tablist" id="myTab">
      <li role="presentation" class="content-desc"><a class="desc" href="#desc" aria-controls="desc" role="tab" data-toggle="tab">内容</a></li>
      <li role="presentation" class="content-author"><a class="author" href="#author" aria-controls="author" role="tab" data-toggle="tab">作者</a></li>
      <li role="presentation" class="content-comment"><a class="comment" href="#comment" aria-controls="comment" role="tab" data-toggle="tab">评论</a></li>
      <li role="presentation" class="content-tag"><a class="tag" href="#tag" aria-controls="tag" role="tab"

<details>
<summary>英文:</summary>

You can keep only one click event for all tabs and inside that using `$(this)` and `id` show or hide your tabs.

***Demo Code :***

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    $(&quot;#myTab li &gt; a &quot;).click(function(e) {
      e.preventDefault();
      $(&quot;#myTab li &gt; a ,.tab-pane&quot;).removeClass(&quot;active&quot;);
      $(this).addClass(&quot;active&quot;)
      $(&quot;.tab-pane&quot;).hide() //hide all other tabs
      $(&quot;.tab-content &quot; + $(this).attr(&quot;href&quot;)).addClass(&quot;active&quot;).show(); //show curent 
    })

    $(&#39;#myTab &gt; li:eq(0) a&#39;).click() // trigger click on first a

&lt;!-- language: lang-css --&gt;

    .content_main {
      width: 650px;
      border: 1px solid #00000033;
      text-align: justify;
      margin: 0px 5px 10px 5px;
      padding: 20px;
      border-radius: 5px;
      line-height: 35px;
    }

    .nav_main .active {
      background: #b421f3;
    }

    .content-tab {
      border: none;
    }

    .nav-tabs&gt;li&gt;a {
      background: #e2e2e2;
      border: 1px solid #00000033;
      box-shadow: 0 -2px 0 #2196f3;
      padding: 14px;
      color: #000;
      margin: 0px 15px 0px 5px;
      border-radius: 5px 5px 0 0;
    }

    .content-tab {
      margin-top: 20px;
    }

    .content-tab a:hover {
      text-decoration: none;
    }

    .content-comment {
      margin-right: -10px;
    }

&lt;!-- language: lang-html --&gt;

    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
    &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;




    &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;nav_main&quot;&gt;
        &lt;!-- Nav tabs --&gt;
        &lt;ul class=&quot;nav nav-tabs content-tab&quot; role=&quot;tablist&quot; id=&quot;myTab&quot;&gt;
          &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;
          &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;
          &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;
          &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;
        &lt;/ul&gt;
        &lt;!-- Tab panes --&gt;
        &lt;div class=&quot;tab-content content_main&quot;&gt;
          &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane active&quot; id=&quot;desc&quot;&gt;
            desc desc desc desc desc desc desc
          &lt;/div&gt;
          &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;author&quot;&gt;
            author author author author author
          &lt;/div&gt;
          &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;comment&quot;&gt;
            comment comment comment comment comment comment
          &lt;/div&gt;
          &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;tag&quot;&gt;
            tag tag tag tag tag tag
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

&lt;!-- end snippet --&gt;



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 .

***Demo Code*** :

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    const triggerTabList = document.querySelectorAll(&#39;#myTab a&#39;)
    triggerTabList.forEach(triggerEl =&gt; {
      const tabTrigger = new bootstrap.Tab(triggerEl)

      triggerEl.addEventListener(&#39;click&#39;, event =&gt; {
        event.preventDefault()
        tabTrigger.show()
      })
    })

    document.querySelector(&#39;#myTab &gt; li:first-child a&#39;).click()

&lt;!-- language: lang-css --&gt;

    .content_main {
      width: 650px;
      border: 1px solid #00000033;
      text-align: justify;
      margin: 0px 5px 10px 5px;
      padding: 20px;
      border-radius: 5px;
      line-height: 35px;
    }

    .nav_main .active {
      background: #b421f3;
    }

    .content-tab {
      border: none;
    }

    .nav-tabs&gt;li&gt;a {
      background: #e2e2e2;
      border: 1px solid #00000033;
      box-shadow: 0 -2px 0 #2196f3;
      padding: 14px;
      color: #000;
      margin: 0px 15px 0px 5px;
      border-radius: 5px 5px 0 0;
    }

    .content-tab {
      margin-top: 20px;
    }

    .content-tab a:hover {
      text-decoration: none;
    }

    .content-comment {
      margin-right: -10px;
    }

&lt;!-- language: lang-html --&gt;

    &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;
    &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;



    &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;nav_main&quot;&gt;
        &lt;!-- Nav tabs --&gt;
        &lt;ul class=&quot;nav nav-tabs content-tab&quot; role=&quot;tablist&quot; id=&quot;myTab&quot;&gt;
          &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;
          &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;
          &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;
          &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;
        &lt;/ul&gt;
        &lt;!-- Tab panes --&gt;
        &lt;div class=&quot;tab-content content_main&quot;&gt;
          &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane active&quot; id=&quot;desc&quot;&gt;
            desc desc desc desc desc desc desc
          &lt;/div&gt;
          &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;author&quot;&gt;
            author author author author author
          &lt;/div&gt;
          &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;comment&quot;&gt;
            comment comment comment comment comment comment
          &lt;/div&gt;
          &lt;div role=&quot;tabpanel&quot; class=&quot;tab-pane&quot; id=&quot;tag&quot;&gt;
            tag tag tag tag tag tag
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

&lt;!-- end snippet --&gt;



</details>



# 答案2
**得分**: 1

你可以在不使用jQuery的情况下制作这个以下是翻译后的部分

```html
<!-- 开始片段:js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-css -->
.tab-content {
    max-width: 650px;
    border: 1px solid #00000033;
    text-align: justify;
    margin: 0px 5px 10px 5px;
    padding: 20px;
    border-radius: 5px;
    line-height: 35px;
}

.nav-link.active {
    background-color: #b421f3 !important;
}

.nav-tabs {
    border: none;
}

.nav-tabs > li > .nav-link {
    background: #e2e2e2;
    border: 1px solid #00000033;
    box-shadow: 0 -2px 0 #2196f3;
    padding: 14px;
    color: #000;
    margin: 0px 15px 0px 5px;
    border-radius: 5px 5px 0 0;
}

.nav-tabs {
    margin-top: 20px;
}

.nav-link:hover {
    text-decoration: none;
}

<!-- 语言: lang-html -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>

<div class="container">
  <div class="nav_main">
    <!-- 导航标签 -->
    <ul class="nav nav-tabs" role="tablist">
      <li class="nav-item" role="presentation">
        <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>
      </li>
      <li class="nav-item" role="presentation">
        <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>
      </li>
      <li class="nav-item" role="presentation">
        <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>
      </li>
      <li class="nav-item" role="presentation">
        <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>
      </li>
    </ul>
                
    <!-- 标签面板 -->
    <div class="tab-content">
      <div class="tab-pane fade show active" id="content" role="tabpanel" aria-labelledby="content-tab" tabindex="0">
        desc desc desc desc desc desc desc
      </div>
      <div class="tab-pane fade" id="author" role="tabpanel" aria-labelledby="author-tab" tabindex="0">
        author author author author author
      </div>
      <div class="tab-pane fade" id="comment" role="tabpanel" aria-labelledby="comment-tab" tabindex="0">
        comment comment comment comment comment comment
      </div>
      <div class="tab-pane fade" id="tag" role="tabpanel" aria-labelledby="tag-tab" tabindex="0">
        tag tag tag tag tag tag
      </div>
    </div>
  </div>
</div>

<!-- 结束片段 -->
英文:

you can make thit without Jquery

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

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

.tab-content {
max-width: 650px;
border: 1px solid #00000033;
text-align: justify;
margin: 0px 5px 10px 5px;
padding: 20px;
border-radius: 5px;
line-height: 35px;
}
.nav-link.active {
background-color: #b421f3 !important;
}
.nav-tabs {
border: none;
}
.nav-tabs &gt; li &gt; .nav-link {
background: #e2e2e2;
border: 1px solid #00000033;
box-shadow: 0 -2px 0 #2196f3;
padding: 14px;
color: #000;
margin: 0px 15px 0px 5px;
border-radius: 5px 5px 0 0;
}
.nav-tabs {
margin-top: 20px;
}
.nav-link:hover {
text-decoration: none;
}

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

&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;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js
&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;nav_main&quot;&gt;
&lt;!-- Nav tabs --&gt;
&lt;ul class=&quot;nav nav-tabs&quot; role=&quot;tablist&quot;&gt;
&lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;
&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;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;
&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;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;
&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;
&lt;/li&gt;
&lt;li class=&quot;nav-item&quot; role=&quot;presentation&quot;&gt;
&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;
&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- Tab panes --&gt;
&lt;div class=&quot;tab-content&quot;&gt;
&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;
desc desc desc desc desc desc desc
&lt;/div&gt;
&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;
author author author author author
&lt;/div&gt;
&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;
comment comment comment comment comment comment
&lt;/div&gt;
&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;
tag tag tag tag tag tag
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&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:

确定