【问题标题】:All tabs are showing, but I would like only one tab to show at any given time所有选项卡都显示了,但我希望在任何给定时间只显示一个选项卡
【发布时间】:2018-04-04 09:36:34
【问题描述】:

有人可以看看下面的 javascript 吗?

我想创建 3 个选项卡,在任何给定时间只显示一个选项卡。

目前,在测试以下代码时,默认情况下 3 个选项卡会在彼此下方显示(而不是仅显示一个),并且只有在选择了一个选项卡后,其他选项卡才会隐藏。

<div class="tab">

  <button class="tablinks" onclick="openCity(event, 'General')">
    <span size="4" style="font-size: small;">
      <span color="grey" style="color: grey;">
        <b>General</b>
      </span>
    </span>
  </button>

  <button class="tablinks" onclick="openCity(event, 'Product Specifications')">
    <span size="4" style="font-size: small;">
      <span color="grey" style="color: grey;">
        <b>Product Specifications</b>
      </span>
    </span>
  </button>

  <button class="tablinks" onclick="openCity(event, 'Guarantee')">
    <span size="4" style="font-size: small;">
      <span color="grey" style="color: grey;">
        <b>Guarantee</b>
      </span>
    </span>
  </button>

</div>

<div id="General" class="tabcontent">
  CONTENT
</div>

<div id="Product Specifications" class="tabcontent">
  CONTENT
</div>

<div id="Guarantee" class="tabcontent">
  CONTENT
</div>

<script>// <![CDATA[
  function openCity(evt, cityName) {
    // Declare all variables
    var i, tabcontent, tablinks;

    // Get all elements with class="tabcontent" and hide them
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
      tabcontent[i].style.display = "none";
    }

    // Get all elements with class="tablinks" and remove the class "active"
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
      tablinks[i].className = tablinks[i].className.replace(" active", "");
    }

    // Show the current tab, and add an "active" class to the button that opened the tab
    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
  }
  // ]]>
</script>

【问题讨论】:

    标签: javascript html tabs


    【解决方案1】:
     by default we are showing first tab, on click of the other tabs data will be shown accordingly, working demo is shown below
    

    Could someone take a look at the javascript below? I would like to create 3 tabs, with only one tab showing at any given time, when selected. Right now, when testing the below code, the 3 tabs show by default under each other (as opposed to only one)
    and only once a tab is selected, the other tabs are hidden.
    <style>
    
    </style>
    <div class="tab">
      <button class="tablinks" onclick="openCity(event, 'London')">London</button>
      <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
      <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
    </div>
    
    <div id="London" class="tabcontent">
      <h3>London</h3>
      <p>London is the capital city of England.</p>
    </div>
    
    <div id="Paris" class="tabcontent">
      <h3>Paris</h3>
      <p>Paris is the capital of France.</p>
    </div>
    
    <div id="Tokyo" class="tabcontent">
      <h3>Tokyo</h3>
      <p>Tokyo is the capital of Japan.</p>
    </div>
    
    
    <div id="General" class="tabcontent">
      General CONTENT
    </div>
    
    <div id="Product Specifications" class="tabcontent">
      Product Specifications CONTENT
    </div>
    
    <div id="Guarantee" class="tabcontent">
      GuaranteeCONTENT
    </div>
    
    <script>
      // Get all elements with class="tabcontent" and hide them
      var tabcontent = document.getElementsByClassName("tabcontent");
      for (i = 1; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
      }
      // <![CDATA[
      function openCity(evt, cityName) {
        // Declare all variables
        var i, tabcontent, tablinks;
    
        var tabcontent = document.getElementsByClassName("tabcontent");
        for (i = 0; i < tabcontent.length; i++) {
          tabcontent[i].style.display = "none";
        }
    
        // Get all elements with class="tablinks" and remove the class "active"
        tablinks = document.getElementsByClassName("tablinks");
        for (i = 0; i < tablinks.length; i++) {
          tablinks[i].className = tablinks[i].className.replace(" active", "");
        }
    
        // Show the current tab, and add an "active" class to the button that opened the tab
        document.getElementById(cityName).style.display = "block";
        evt.currentTarget.className += " active";
      }
      // ]]>
    </script>

    【讨论】:

    • 您好,谢谢。这很有帮助。但是,有没有办法让第一个选项卡默认显示?并且只隐藏第二个和第三个选项卡的内容,直到被点击。但是当点击时,应该只显示所选标签的内容。
    • 我已根据您的要求编辑了答案(在加载时显示第一个标签)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2018-05-05
    相关资源
    最近更新 更多