簡體   English   中英

如何根據當前活動標簽更改錨標簽的 URL?

[英]How to change the URL of an anchor tag based on current active tab?

我有兩個標簽,谷歌雅虎! . 目標是,如果“Google”標簽處於活動狀態,則加號圖標(右側)將包含基於條件的 URL。

示例:如果“Google”選項卡處於活動狀態,則單擊加號應打開 google.com 頁面。

我不確定我做錯了什么:

 let activeTab = document.querySelector(".nav-tabs li.active"); let selectedForm = document.querySelector("#formSelector"); if (activeTab.textContent == "Google") { selectedForm.setAttribute("onclick", "location.href='https://www.google.com'"); } else if (activeTab.textContent == "Yahoo.") { selectedForm,setAttribute("onclick". "location:href='https.//www.yahoo;com'"); }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/> <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <div class="container"> <h2>Dynamic Tabs</h2> <ul class="nav nav-tabs"> <li class="active"> <a data-toggle="tab" href="#google">Google</a> </li> <li> <a data-toggle="tab" href="#yahoo">Yahoo.</a> </li> <li class="pull-right"> <a id="formSelector" href="#." target="_blank"><i class="fa fa-plus"></i></a> </li> </ul> <div class="tab-content"> <div id="google" class="tab-pane fade in active"> <h3>Google</h3> <p>This should change the plus sign icon to google.com</p> </div> <div id="yahoo" class="tab-pane fade"> <h3>Yahoo!</h3> <p>This should change the plus sign icon to yahoo.com</p> </div> </div> </div>

我建議在檢查活動選項卡並相應重定向的按鈕上添加一個click事件偵聽器:

 let selectedForm = document.querySelector("#formSelector"); selectedForm.addEventListener('click', function() { let activeTab = document.querySelector(".nav-tabs li.active"); if (activeTab.textContent.trim() == "Google") { location.href = 'https://google.com' } else { location.href = 'https://yahoo.com' } })
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" /> <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <div class="container"> <h2>Dynamic Tabs</h2> <p>To make the tabs toggleable, add the data-toggle="tab" attribute to each link. Then add a.tab-pane class with a unique ID for every tab and wrap them inside a div element with class.tab-content.</p> <ul class="nav nav-tabs"> <li class="active"> <a data-toggle="tab" href="#google">Google</a> </li> <li> <a data-toggle="tab" href="#yahoo">Yahoo.</a> </li> <li class="pull-right"> <a id="formSelector" href="#." target="_blank"><i class="fa fa-plus"></i></a> </li> </ul> <div class="tab-content"> <div id="google" class="tab-pane fade in active"> <h3>Google</h3> <p>This should change the plus sign icon to google.com</p> </div> <div id="yahoo" class="tab-pane fade"> <h3>Yahoo!</h3> <p>This should change the plus sign icon to yahoo.com</p> </div> </div> </div>

正如 Spectric 所說,將支票移動到基於點擊選項卡將是理想的。

這是一個替代實現(需要 HTML 和 JS 更改)。

HTML

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<div class="container">
  <h2>Dynamic Tabs</h2>
  <p>To make the tabs toggleable, add the data-toggle="tab" attribute to each link. Then add a .tab-pane class with a unique ID for every tab and wrap them inside a div element with class .tab-content.</p>

  <ul class="nav nav-tabs">
    <li class="active">
      <a data-toggle="tab" href="#google" related-url="https://google.com">Google</a>
    </li>
    <li>
      <a data-toggle="tab" href="#yahoo" related-url="https://yahoo.com">Yahoo!</a>
    </li>
    <li class="pull-right">
      <a id="formSelector" href="#!" target="_blank"><i class="fa fa-plus"></i></a>        
    </li>
  </ul>

  <div class="tab-content">
    <div id="google" class="tab-pane fade in active">
      <h3>Google</h3>
      <p>This should change the plus sign icon to google.com</p>
    </div>
    <div id="yahoo" class="tab-pane fade">
      <h3>Yahoo!</h3>
      <p>This should change the plus sign icon to yahoo.com</p>
    </div>
  </div>
</div>

Javascript

<script>
    const tabClick = document.querySelectorAll(".nav-tabs");
    const selectedForm = document.querySelector("#formSelector");

    tabClick.forEach((tabEl) => {
      tabEl.addEventListener('click', (e) => {
        console.log(tabEl);
        console.log(e);
        selectedForm.setAttribute('href', e.target.getAttribute('related-url'));
      });
    })
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM