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

厄运

下面的示例有一个名为 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

连接器

您希望根据.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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

jQuery [选项卡]打开和关闭

Bootstrap嵌套的导航选项卡在重新加载时不会打开相同的选项卡

多个嵌套选项卡

打开单击的选项卡并关闭其余选项卡

打开选项卡,关闭选项卡,然后使用Selenium在Mac上退出Firefox?

在不关闭实际选项卡的情况下打开选项卡

JavaFX中选项卡关闭时的两次选项卡更改事件

在IntelliJ IDEA中关闭选项卡时更改为最近使用的选项卡?

动态创建的选项卡 - 选项卡关闭时销毁对象

删除所有选项卡上选项卡关闭时的sessionStorage属性

选项卡引导中选项卡中选项卡中的嵌套选项卡

打开“滑动选项卡布局”活动时如何设置特定的选项卡?

打开一个选项卡时如何删除其他选项卡

Javascript 功能未关闭打开的选项卡

鼠标动作有趣,打开/关闭选项卡

关闭新打开的选项卡 - Python (Selenium)

嵌套选项卡不会在 HTML 中关闭

仅在尚未打开新选项卡时打开

如何在此手风琴中打开默认选项卡,并在打开另一个选项卡时将其关闭?

用户单击关闭选项卡时如何打开新窗口浏览器?

如何在单击使用 _blank <a> 目标属性打开的按钮时关闭新选项卡

选项卡关闭时未出现 Web 推送通知

更改选项卡时关闭以模态显示的 ViewController

单击时关闭手风琴选项卡

删除选项卡时无法关闭工作表

再次单击时关闭javascript选项卡

jQuery,再次单击时关闭选项卡菜单

单击活动按钮时关闭选项卡(无 jQuery)

输入为选项卡时如何打开新表格?