代码之家  ›  专栏  ›  技术社区  ›  Tncmk

可单击按钮移动另一个选项卡

  •  0
  • Tncmk  · 技术社区  · 5 年前

    https://www.w3schools.com/howto/howto_js_tab_header.asp . 它工作得很好,直到我试图在选项卡中创建一个按钮,点击后指向另一个选项卡。我试着把东京标签的按钮加到伦敦标签上。它直接进入该选项卡,但不幸的是,直接选项卡按钮背景没有改变,我知道为什么它没有改变,但我没有一个解决方案。非常感谢你的帮助

    function openCity(cityName,elmnt,color) {
      var i, tabcontent, tablinks;
      tabcontent = document.getElementsByClassName("tabcontent");
      for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
      }
      tablinks = document.getElementsByClassName("tablink");
      for (i = 0; i < tablinks.length; i++) {
        tablinks[i].style.backgroundColor = "";
      }
      document.getElementById(cityName).style.display = "block";
      elmnt.style.backgroundColor = color;
    
    }
    // Get the element with id="defaultOpen" and click on it
    document.getElementById("defaultOpen").click();
    body {font-family: "Lato", sans-serif;}
    
    .tablink {
      background-color: #555;
      color: white;
      float: left;
      border: none;
      outline: none;
      cursor: pointer;
      padding: 14px 16px;
      font-size: 17px;
      width: 25%;
    }
    
    .tablink:hover {
      background-color: #777;
    }
    
    /* Style the tab content */
    .tabcontent {
      color: white;
      display: none;
      padding: 50px;
      text-align: center;
    }
    
    #London {background-color:red;}
    #Paris {background-color:green;}
    #Tokyo {background-color:blue;}
    #Oslo {background-color:orange;}
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    </head>
    <body>
    
    <p>Click on the buttons inside the tabbed menu:</p>
    
    <div id="London" class="tabcontent">
      <h1>London</h1>
      <p>London is the capital city of England.</p>
      <button class="tablink" onclick="openCity('Tokyo', this, 'blue')">Tokyo</button>
    </div>
    
    <div id="Paris" class="tabcontent">
      <h1>Paris</h1>
      <p>Paris is the capital of France.</p> 
    </div>
    
    <div id="Tokyo" class="tabcontent">
      <h1>Tokyo</h1>
      <p>Tokyo is the capital of Japan.</p>
    </div>
    
    <div id="Oslo" class="tabcontent">
      <h1>Oslo</h1>
      <p>Oslo is the capital of Norway.</p>
    </div>
    
    <button class="tablink" onclick="openCity('London', this, 'red')" id="defaultOpen">London</button>
    <button class="tablink" onclick="openCity('Paris', this, 'green')">Paris</button>
    <button class="tablink" onclick="openCity('Tokyo', this, 'blue')">Tokyo</button>
    <button class="tablink" onclick="openCity('Oslo', this, 'orange')">Oslo</button>
    
       
    </body>
    </html>
    1 回复  |  直到 5 年前
        1
  •  1
  •   Simone Rossaini    5 年前

    代码运行良好,因为您的函数将更改您单击的按钮的颜色,所以我为内部按钮创建了另一个函数。 例子:

    function openCity(cityName,elmnt,color) {
      var i, tabcontent, tablinks;
      tabcontent = document.getElementsByClassName("tabcontent");
      for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
      }
      tablinks = document.getElementsByClassName("tablink");
      for (i = 0; i < tablinks.length; i++) {
        tablinks[i].style.backgroundColor = "";
      }
      document.getElementById(cityName).style.display = "block";
      elmnt.style.backgroundColor = color;
    
    }
    // Get the element with id="defaultOpen" and click on it
    document.getElementById("defaultOpen").click();
    
    function openCityInt(el){
        const x = document.getElementsByClassName(el);
        x[0].click();
    }
    body {font-family: "Lato", sans-serif;}
    
    .tablink {
      background-color: #555;
      color: white;
      float: left;
      border: none;
      outline: none;
      cursor: pointer;
      padding: 14px 16px;
      font-size: 17px;
      width: 25%;
    }
    
    .tablink:hover {
      background-color: #777;
    }
    
    /* Style the tab content */
    .tabcontent {
      color: white;
      display: none;
      padding: 50px;
      text-align: center;
    }
    
    #London {background-color:red;}
    #Paris {background-color:green;}
    #Tokyo {background-color:blue;}
    #Oslo {background-color:orange;}
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    </head>
    <body>
    
    <p>Click on the buttons inside the tabbed menu:</p>
    
    <div id="London" class="tabcontent">
      <h1>London</h1>
      <p>London is the capital city of England.</p>
      <button class="tablink" onclick="openCityInt('tokyo')">Tokyo</button>
    </div>
    
    <div id="Paris" class="tabcontent">
      <h1>Paris</h1>
      <p>Paris is the capital of France.</p> 
    </div>
    
    <div id="Tokyo" class="tabcontent">
      <h1>Tokyo</h1>
      <p>Tokyo is the capital of Japan.</p>
    </div>
    
    <div id="Oslo" class="tabcontent">
      <h1>Oslo</h1>
      <p>Oslo is the capital of Norway.</p>
    </div>
    
    <button class="tablink london" onclick="openCity('London', this, 'red')" id="defaultOpen">London</button>
    <button class="tablink paris" onclick="openCity('Paris', this, 'green')">Paris</button>
    <button class="tablink tokyo" onclick="openCity('Tokyo', this, 'blue')">Tokyo</button>
    <button class="tablink oslo" onclick="openCity('Oslo', this, 'orange')">Oslo</button>
    
       
    </body>
    </html>