下面的示例有一个名为 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] 删除。
我来说两句