javascript - W3.CSS 导航选项卡 - 显示 2 id

标签 javascript html css w3-css

我正在使用 W3.CSS 导航选项卡,但遇到问题。 我有 2 个 id,我想在人们单击选项卡“伦敦”然后显示 2 个 id 时显示这 2 个 id。 在 W3.CSS 导航选项卡代码中,我只能同时显示 1 个 id,但我不知道如何添加另外 1 个 id 来显示

W3.CSS 导航选项卡代码

<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>

<div class="w3-container">
  <h2>Tabs in a Grid</h2>

  <div class="w3-row">
    <a href="javascript:void(0)" onclick="openCity(event, 'London');">
      <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">London</div>
    </a>
    <a href="javascript:void(0)" onclick="openCity(event, 'Paris');">
      <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">Paris</div>
    </a>
    <a href="javascript:void(0)" onclick="openCity(event, 'Tokyo');">
      <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">Tokyo</div>
    </a>
  </div>

  <div id="London" class="w3-container city" style="display:none">
    <h2>London</h2>
    <p>London is the capital city of England.</p>
  </div>

  <div id="Paris" class="w3-container city" style="display:none">
    <h2>Paris</h2>
    <p>Paris is the capital of France.</p> 
  </div>

  <div id="Tokyo" class="w3-container city" style="display:none">
    <h2>Tokyo</h2>
    <p>Tokyo is the capital of Japan.</p>
  </div>
</div>

<script>
function openCity(evt, cityName) {
  var i, x, tablinks;
  x = document.getElementsByClassName("city");
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablink");
  for (i = 0; i < x.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" w3-border-red", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.firstElementChild.className += " w3-border-red";
}
</script>

</body>
</html>

最佳答案

一个简单的解决方案是将多个城市名称作为数组而不是单个城市名称传递给 openCity 函数。

在以下示例中,当您单击 London 选项卡时,将显示 id 为 LondonParis 的 2 个选项卡的内容.

function openCity(evt, cityNames) {
    let i, x, tablinks;
    
    x = document.getElementsByClassName("city");
    for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
    }
    
    tablinks = document.getElementsByClassName("tablink");
    for (i = 0; i < x.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" w3-border-red", "");
    }
  
    cityNames.map(function(city) {
        document.getElementById(city).style.display = "block";
    });
    
    evt.currentTarget.firstElementChild.className += " w3-border-red";
}
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<div class="w3-container">
    <h2>Tabs in a Grid</h2>

    <div class="w3-row">
        <a href="javascript:void(0)" onclick="openCity(event, ['London','Paris']);">
            <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding w3-border-red">London</div>
        </a>
        <a href="javascript:void(0)" onclick="openCity(event, ['Paris']);">
            <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">Paris</div>
        </a>
        <a href="javascript:void(0)" onclick="openCity(event, ['Tokyo']);">
            <div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">Tokyo</div>
        </a>
    </div>

    <div id="London" class="w3-container city">
        <h2>London</h2>
        <p>London is the capital city of England.</p>
    </div>

    <div id="Paris" class="w3-container city" style="display:none">
        <h2>Paris</h2>
        <p>Paris is the capital of France.</p> 
    </div>

    <div id="Tokyo" class="w3-container city" style="display:none">
        <h2>Tokyo</h2>
        <p>Tokyo is the capital of Japan.</p>
    </div>
</div>

关于javascript - W3.CSS 导航选项卡 - 显示 2 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60020623/

相关文章:

javascript - 找到某些单词并替换它们

javascript - addEventListener 无法正常工作

javascript - 地址文本区域元素不会在 Firefox 中自动填充

android - Webview 无法正确呈现 css - Kitkat 版本 4.4.2

html - 想要通过单击复选框来按 id/class 隐藏按钮

html - 图片显示在不同的地方取决于浏览器

javascript - 移动二维数组中的行和列 - Javascript

javascript - 预加载背景图片

javascript - 使用 ReadableStream 作为 HTML <img> 源

php - 如何在不拉伸(stretch)的情况下以特定尺寸显示图像?