gpt4 book ai didi

javascript - 如何自动显示此菜单的第一个结果?

转载 作者:行者123 更新时间:2023-11-29 20:58:21 27 4
gpt4 key购买 nike

我的这个菜单有问题,当我进入我的网站时,如果我点击菜单,它只显示菜单“电影”中的内容,当我进入该网站时,它什么也没有显示,我想要相反的那,他会显示“电影”菜单的内容而无需单击它,我做错了什么?

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";
}
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}

.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}

.tab button:hover {
background-color: #ddd;
}

.tab button.active {
background-color: #ccc;
}

.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'Filmes')">Filmes</button>
<button class="tablinks" onclick="openCity(event, 'Series')">Séries</button>
<button class="tablinks" onclick="openCity(event, 'Animes')">Animes</button>
<button class="tablinks" onclick="openCity(event, 'Desenhos')">Desenhos</button>
</div>

<div id="Filmes" class="tabcontent">
<h3>Filmes</h3>
<p>Todos os filmes</p>
</div>

<div id="Series" class="tabcontent">
<h3>Séries</h3>
<p>Todas as séries</p>
</div>

<div id="Animes" class="tabcontent">
<h3>Animes</h3>
<p>Todos os animes</p>
</div>

<div id="Desenhos" class="tabcontent">
<h3>Desenhos</h3>
<p>Todos os desenhos</p>
</div>

最佳答案

我相信这可能就是您要找的。基本上,我更改了 Filmes 按钮的初始类以包含 active 并另外确保 Filmes 部分包含一个 display: block 的风格。

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";
}
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}

.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}

.tab button:hover {
background-color: #ddd;
}

.tab button.active {
background-color: #ccc;
}

.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
<div class="tab">
<button class="tablinks active" onclick="openCity(event, 'Filmes')">Filmes</button>
<button class="tablinks" onclick="openCity(event, 'Series')">Séries</button>
<button class="tablinks" onclick="openCity(event, 'Animes')">Animes</button>
<button class="tablinks" onclick="openCity(event, 'Desenhos')">Desenhos</button>
</div>

<div style="display: block" id="Filmes" class="tabcontent">
<h3>Filmes</h3>
<p>Todos os filmes</p>
</div>

<div id="Series" class="tabcontent">
<h3>Séries</h3>
<p>Todas as séries</p>
</div>

<div id="Animes" class="tabcontent">
<h3>Animes</h3>
<p>Todos os animes</p>
</div>

<div id="Desenhos" class="tabcontent">
<h3>Desenhos</h3>
<p>Todos os desenhos</p>
</div>

关于javascript - 如何自动显示此菜单的第一个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48037916/

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