gpt4 book ai didi

javascript - 如何使用 HTML、CSS 和 JavaScript 让多个选项卡式部分在加载时打开第一个选项卡?

转载 作者:太空宇宙 更新时间:2023-11-04 06:06:03 25 4
gpt4 key购买 nike

我已将多个选项卡式部分添加到我的网页,但我无法确保第二个选项卡式部分在加载时也打开第一个选项卡。我目前正在关注 this来自 W3Schools 的教程,这是我目前在 W3Schools 上的代码:

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;
}
<p>In this example, we use JavaScript to "click" on the London button, to open the tab on page load.</p>

<!--first tab-->

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

<div id="London" class="tabcontent">
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>

<div id="Paris" class="tabcontent">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>

<div id="Tokyo" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>

<br><br><br><br><br>

<!--second tab-->

<div class="tab">
<button class="tablinks" onclick="openCity(event, '1')" id="defaultOpen">London</button>
<button class="tablinks" onclick="openCity(event, '2')">Paris</button>
<button class="tablinks" onclick="openCity(event, '3')">Tokyo</button>
</div>

<div id="1" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</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";
}

// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>

我已经设法在第一个选项卡下方创建了第二个选项卡部分,但我无法让两个选项卡部分同时在页面加载时显示第一个选项卡。

最佳答案

对城市使用类名而不是 ID。向事件内容添加“显示”类名:

function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (var i = 0; i < tabcontent.length; i++) {
tabcontent[i].classList.remove('show');
}
tablinks = document.getElementsByClassName("tablinks");
for (var i = 0; i < tablinks.length; i++) {
tablinks[i].classList.remove('active');
}
document.getElementsByClassName(cityName)[0].classList.add('show');
document.getElementsByClassName(cityName)[1].classList.add('show');
var cuttentTabClass = evt.currentTarget.className.split(" ")[1];
console.log(cuttentTabClass);
document.getElementsByClassName(cuttentTabClass)[0].classList.add('active');
document.getElementsByClassName(cuttentTabClass)[1].classList.add('active');
}

// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
.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;
}
.tabcontent.show {
display: block;
}
<p>In this example, we use JavaScript to "click" on the London button, to open the tab on page load.</p>

<!--first tab-->

<div class="tab">
<button class="tablinks l" onclick="openCity(event, 'london')" id="defaultOpen">London</button>
<button class="tablinks p" onclick="openCity(event, 'paris')">Paris</button>
<button class="tablinks t" onclick="openCity(event, 'tokyo')">Tokyo</button>
</div>

<div class="tabcontent london">
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>

<div class="tabcontent paris">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>

<div class="tabcontent tokyo">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>

<br><br><br><br><br>

<!--second tab-->

<div class="tab">
<button class="tablinks l" onclick="openCity(event, 'london')">London</button>
<button class="tablinks p" onclick="openCity(event, 'paris')">Paris</button>
<button class="tablinks t" onclick="openCity(event, 'tokyo')">Tokyo</button>
</div>

<div class="tabcontent london">
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>

<div class="tabcontent paris">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>

<div class="tabcontent tokyo">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>

关于javascript - 如何使用 HTML、CSS 和 JavaScript 让多个选项卡式部分在加载时打开第一个选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56857419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com