我在网页上添加了多个选项卡式部分,但是我无法确保第二个选项卡式部分在加载时也打开了第一个选项卡。我目前正在关注W3Schools的this教程,到目前为止,这是我在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/