【问题标题】:Tab closes when opening nested tab打开嵌套选项卡时选项卡关闭
【发布时间】:2021-04-17 11:47:33
【问题描述】:

下面的示例有一个名为 London 的选项卡,其中包含另一个名为 Paris 的选项卡。如何在不关闭伦敦标签的情况下打开巴黎?

该示例直接来自 W3SCHOOL,但稍作修改以适合我的用例。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial;}

/* Style the tab */
.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
  font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}
</style>
</head>
<body>

<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'London')">London</button>
</div>


<div id="London" class="tabcontent">
  <div class="tab">
  <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
  </div>
  <div id="Paris" class="tabcontent" >
    <h3>Paris</h3>
    <p>Paris is the capital of France.</p> 
  </div>
  <h3>London</h3>
  <p>London is the capital city of England.</p>
</div>

<script>
function openCity(evt, cityName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}
</script>
   
</body>
</html> 

我认为问题在于“活跃”类从伦敦消失了,因此我一按巴黎就关闭了。但我不擅长 jQuery。

我猜它可以很容易地在 W3 界面上测试: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_tabs

【问题讨论】:

  • 感谢运行代码片段编辑
  • 你的回答很好!

标签: html jquery css


【解决方案1】:

您希望根据 .tabcontent 元素集合是否与您的目标选项卡共享相同的父元素来过滤它们。

我还稍微重构了一些代码的其他部分,请比较一下,变化是不言自明的。

function openCity(evt, cityName) {
  let i, tabcontent, tablinks;
  const targetTab = document.querySelector(`#${cityName}.tabcontent`);
  tabcontent = [...document.getElementsByClassName("tabcontent")].filter(el => el.parentElement === targetTab.parentElement);
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].hidden = true;
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].classList.remove("active");
  }
  targetTab.style.display = "block";
  evt.currentTarget.classList.add("active");
}
body {
  font-family: Arial;
}


/* Style the tab */

.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
}


/* Style the buttons inside the tab */

.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
  font-size: 17px;
}


/* Change background color of buttons on hover */

.tab button:hover {
  background-color: #ddd;
}


/* Create an active/current tablink class */

.tab button.active {
  background-color: #ccc;
}


/* Style the tab content */

.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}
<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'London')">London</button>
</div>


<div id="London" class="tabcontent">
  <div class="tab">
    <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
  </div>
  <div id="Paris" class="tabcontent">
    <h3>Paris</h3>
    <p>Paris is the capital of France.</p>
  </div>
  <h3>London</h3>
  <p>London is the capital city of England.</p>
</div>

【讨论】:

    【解决方案2】:

    使用 classList 的替代解决方案。 可以使用可选按钮清除所有显示。

    // filterSelection("all");  // optional initial display
    
    function filterSelection(c) {
      sel = document.getElementsByClassName("filterDiv");
      for (let el of sel) { 
        el.classList.remove('show');
        if (el.classList.contains(c) || c == 'all') el.classList.add('show');
      } 
    }
    
    // Add active class to the current button (highlight it)
    var btns = document.querySelectorAll('#btnContainer .btn');
    for (var i = 0; i < btns.length; i++) {
      btns[i].addEventListener("click", (evt) => {
        for (let j=0; j<btns.length; j++) { btns[j].classList.remove('active'); }
        evt.currentTarget.classList.toggle("active");
      });
    }
     .filterDiv {
       background-color: #2196F3;
       color: #ffffff;
       margin: 2px;
       display: none;
    
       padding: 6px 12px;
       border: 1px solid #ccc;
       border-top: none;
     }
     .show { display: block; }
     .container {
       margin-top: 2px;
       overflow: hidden;
     }
    /* Style the buttons */
     .btn {
       font-size: larger;
       border: none;
       outline: none;
       padding: 12px 16px;
       background-color: #f1f1f1;
       cursor: pointer;
     }
     .btn:hover { background-color: #ddd; }
     .btn.active {
       background-color: #666;
       color: white;
     }
    <h2>City Countries</h2>
    
    <div id="btnContainer">
      <button class="btn" onclick="filterSelection('London')"> London </button>
      <button class="btn" onclick="filterSelection('Paris')"> Paris </button>
      <button class="btn" onclick="filterSelection('Tokyo')"> Tokyo </button>
    
      <button class="btn" onclick="filterSelection('all')"> Show all</button>
    <!--      <button class="btn" onclick="filterSelection('')"> Show none</button> <!-- optional -->
    </div>
    
    <div class="container">
     <div id="London" class="filterDiv London">
      <h3>London</h3>
      <p>London is the capital city of England.</p>
     </div>
     <div id="Paris" class="filterDiv Paris">
      <h3>Paris</h3>
      <p>Paris is the capital of France.</p> 
     </div>
     <div id="Tokyo" class="filterDiv Tokyo">
      <h3>Tokyo</h3>
      <p>Tokyo is the capital of Japan.</p>
     </div>
    </div>

    【讨论】: