我想在每次加载页面时默认显示 TAB1 事件
脚本:
function openCity(evt, cityName) {
// Declare all variables
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tabcontent.length; i++) {
tablinks[i].classList.remove("active");
}
// Show the current tab, and add an "active" class to the link that opened the tab
document.getElementById(cityName).style.display = "block";
evt.currentTarget.classList.add("active");
}
我只需要 tab1 在加载时显示,我怎样才能简单地做到这一点?
最佳答案
脚本
function init() {
reset();
document.getElementsByClassName("tabcontent")[0].style.display = "block";
document.getElementsByClassName("tablinks")[0].classList.add("active");
}
function reset() {
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tabcontent.length; i++) {
tablinks[i].classList.remove("active");
}
}
function openCity(evt, cityName) {
reset();
// Show the current tab, and add an "active" class to the link that opened the tab
document.getElementById(cityName).style.display = "block";
evt.currentTarget.classList.add("active");
}
并在脚本标记中调用 init()。
关于javascript - 如何使用 Javascript 默认显示第一个选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36504542/