提问者:小点点

打开嵌套选项卡时关闭选项卡


下面的示例有一个名为London的选项卡,其中有另一个名为Paris的选项卡。我怎么能在伦敦不结帐的情况下打开巴黎?

该示例直接来自W3SCHOOL,但经过了一些修改以适应我的用例。

null

<!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> 

null

我相信问题是,“活跃”班从伦敦消失,因此,一旦我向巴黎施压,就会关闭。但我不擅长jQuery。

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


共1个答案

匿名用户

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

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

null

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>