开发者问题收集

使用 jQuery 更改 Bootstrap 5 活动选项卡

2022-02-23
2863

我尝试在单击另一个选项卡内的按钮时更改活动选项卡。我找到的所有示例都是带有锚标记的选项卡,没有按钮。我是否可以在不将按钮更改为锚标记的情况下实现此目的?我尝试将我看到的代码应用于锚标记,但没有成功。当我单击查看更多时,我想切换到推荐选项卡

<ul class="nav nav-tabs innerTab" id="allTab" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="all-tab" data-bs-toggle="tab" data-bs-target="#all" type="button" role="tab"
      aria-controls="all" aria-selected="true">All</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="referral-tab" data-bs-toggle="tab" data-bs-target="#referral" type="button" role="tab"
      aria-controls="referral" aria-selected="false">Ref</button>
  </li>

  <li class="nav-item" role="presentation">
    <button class="nav-link" id="profit-tab" data-bs-toggle="tab" data-bs-target="#profit" type="button" role="tab"
      aria-controls="profit" aria-selected="false">Pro</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="merchandise-tab" data-bs-toggle="tab" data-bs-target="#merchandise" type="button"
      role="tab" aria-controls="merchandise" aria-selected="false">Merch</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="withdrawal-tab" data-bs-toggle="tab" data-bs-target="#withdrawal" type="button"
      role="tab" aria-controls="withdrawal" aria-selected="false">History</button>
  </li>
</ul>
<div class="tab-content" id="walletContent">
  <div class="tab-pane show active" id="all" role="tabpanel" aria-labelledby="all-tab">
    <div class="row goal-meter mb-4 g-4">
      <div class="col">
        <div class="card card-transparent">
          <div>
            <a class="v-link-green d-inline-block float-end mt-3 me-3 view-more">View
              more</a>
          </div>
          <div class="card-body p-0 d-flex align-items-center">
            <div class="progress prog-green" data-value='46'>
              <span class="progress-left">
                <span class="progress-bar"></span>
              </span>
              <span class="progress-right">
                <span class="progress-bar"></span>
              </span>
              <div class="progress-value">46%</div>
            </div>
            <div class="progress-title">Referral
              <p class="highlight-green fig">$120,000.00</p>
            </div>

          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="tab-pane" id="referral" role="tabpanel" aria-labelledby="referral-tab">
    Ref
  </div>
  <div class="tab-pane" id="profit" role="tabpanel" aria-labelledby="profit-tab">
    Profit
  </div>
  <div class="tab-pane" id="merchandise" role="tabpanel" aria-labelledby="merchandise-tab">
    Merch
  </div>
  <div class="tab-pane" id="withdrawal" role="tabpanel" aria-labelledby="withdrawal-tab">
    With
  </div>
</div>
1个回答

添加对 id 为“referral-tab”的按钮的引用,当点击 查看更多 链接时,使用 bootrap 的 api 显示它。

  1. 定义代码以显示特定选项卡:
var triggerEl = document.getElementById('referral-tab')
var tabTrigger = new bootstrap.Tab(triggerEl)
  
function activateReferralsTab() {
  bootstrap.Tab.getInstance(triggerEl).show() // Select tab by name
}
  1. activateReferralsTab() 函数附加到 查看更多 链接 onclick 处理程序:
<a
  class="v-link-green d-inline-block float-end mt-3 me-3 view-more"
  onclick="activateReferralsTab()">
  View more
</a>

以下是一个运行示例。

var triggerEl = document.getElementById('referral-tab')
var tabTrigger = new bootstrap.Tab(triggerEl)
  
function activateReferralsTab() {
  bootstrap.Tab.getInstance(triggerEl).show() // Select tab by name
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<ul class="nav nav-tabs innerTab" id="allTab" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="all-tab" data-bs-toggle="tab" data-bs-target="#all" type="button" role="tab" aria-controls="all" aria-selected="true">All</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="referral-tab" data-bs-toggle="tab" data-bs-target="#referral" type="button" role="tab" aria-controls="referral" aria-selected="false">Ref</button>
  </li>

  <li class="nav-item" role="presentation">
    <button class="nav-link" id="profit-tab" data-bs-toggle="tab" data-bs-target="#profit" type="button" role="tab" aria-controls="profit" aria-selected="false">Pro</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="merchandise-tab" data-bs-toggle="tab" data-bs-target="#merchandise" type="button" role="tab" aria-controls="merchandise" aria-selected="false">Merch</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="withdrawal-tab" data-bs-toggle="tab" data-bs-target="#withdrawal" type="button" role="tab" aria-controls="withdrawal" aria-selected="false">History</button>
  </li>
</ul>
<div class="tab-content" id="walletContent">
  <div class="tab-pane show active" id="all" role="tabpanel" aria-labelledby="all-tab">
    <div class="row goal-meter mb-4 g-4">
      <div class="col">
        <div class="card card-transparent">
          <div>
            <a class="v-link-green d-inline-block float-end mt-3 me-3 view-more" onclick="activateReferralsTab()">View more</a>
          </div>
          <div class="card-body p-0 d-flex align-items-center">
            <div class="progress prog-green" data-value='46'>
              <span class="progress-left">
                                                            <span class="progress-bar"></span>
              </span>
              <span class="progress-right">
                                                            <span class="progress-bar"></span>
              </span>
              <div class="progress-value">46%</div>
            </div>
            <div class="progress-title">Referral
              <p class="highlight-green fig">$120,000.00</p>
            </div>

          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="tab-pane" id="referral" role="tabpanel" aria-labelledby="referral-tab">
    Ref
  </div>
  <div class="tab-pane" id="profit" role="tabpanel" aria-labelledby="profit-tab">
    Profit
  </div>
  <div class="tab-pane" id="merchandise" role="tabpanel" aria-labelledby="merchandise-tab">
    Merch
  </div>
  <div class="tab-pane" id="withdrawal" role="tabpanel" aria-labelledby="withdrawal-tab">
    With
  </div>
</div>`enter code here`
Cristián Ormazábal
2022-02-23