Javascript HTML 中的 TABS:默认选择第一个 TAB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38099670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:05:55  来源:igfitidea点击:

TABS in HTML: Select first TAB by default

javascriptcsshtml

提问by KP Joy

I am trying TABBING in HTML. I am using a tutorial from W3SCHOOLS.

我正在尝试在 HTML 中 TABBING。我正在使用W3SCHOOLS的教程。

Following is the source code. It works fine but the problem I am facing is to select the first tab by default which is missing in this tutorial. I want that first tab should be automatically selected when the page loads and the content of that should be displayed automatically.

以下是源代码。它工作正常,但我面临的问题是默认选择本教程中缺少的第一个选项卡。我希望在页面加载时应该自动选择第一个选项卡,并且应该自动显示其内容。

<!DOCTYPE html>
<html>
<style>
body {font-family: "Lato", sans-serif;}

ul.tab {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Float the list items side by side */
ul.tab li {float: left;}

/* Style the links inside the list items */
ul.tab li a {
    display: inline-block;
    color: black;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of links on hover */
ul.tab li a:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
ul.tab li a:focus, .active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    -webkit-animation: fadeEffect 1s;
    animation: fadeEffect 1s;
}

@-webkit-keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

@keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}
</style>
<body>

<h3>Fade in Tabs</h3>

<ul class="tab">
  <li><a href="#" class="tablinks" onclick="openCity(event, 'London')">London</a></li>
  <li><a href="#" class="tablinks" onclick="openCity(event, 'Paris')">Paris</a></li>
  <li><a href="#" class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</a></li>
</ul>

<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>

<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>

What change should I make in Javascript to select the first tab by default? I tried the following line inside for loop but did not work:

默认情况下,我应该在 Javascript 中进行哪些更改以选择第一个选项卡?我在 for 循环中尝试了以下行,但没有用:

tablinks[0].className += " active";

回答by Sanjeev Kumar

In the anchor tag of first LIadd class=activeand in the following update the div with the following:

在第一次LI添加的锚标记中,class=active并在下面更新 div 如下:

div id=London class=tabcontent style=display:block

div id=London class=tabcontent style=display:block

Here is a working JSfiddle

这是一个有效的JSfiddle

回答by TheBreakthrough

A purely javascript way of doing it, just add this to your script

一种纯粹的 javascript 方式,只需将其添加到您的脚本中

document.getElementsByClassName('tablinks')[0].click()

It imitates a click function on your first tab when the page loads

当页面加载时,它会在您的第一个选项卡上模仿点击功能

回答by Mark

<!DOCTYPE html>
<html>
<style>
body {font-family: "Lato", sans-serif;}

ul.tab {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Float the list items side by side */
ul.tab li {float: left;}

/* Style the links inside the list items */
ul.tab li a {
    display: inline-block;
    color: black;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of links on hover */
ul.tab li a:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
ul.tab li a:focus, .active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    -webkit-animation: fadeEffect 1s;
    animation: fadeEffect 1s;
}

@-webkit-keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

@keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}
</style>
<body>

<h3>Fade in Tabs</h3>

<ul class="tab">
  <li><a href="#" class="tablinks" onclick="openCity(event, 'London')">London</a></li>
  <li><a href="#" class="tablinks" onclick="openCity(event, 'Paris')">Paris</a></li>
  <li><a href="#" class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</a></li>
</ul>

<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>

<script>
document.getElementsByClassName('tablinks')[0].click()
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>

added

添加

document.getElementsByClassName('tablinks')[0].click()
在脚本

回答by isherwood

Add this to the CSS:

将此添加到 CSS:

div[class*="tabcontent"]:first-of-type {
    display: block;
}

Update the markup to this:

将标记更新为:

<a href="#" class="tablinks active" onclick="openCity(event, 'London')">London</a>

Demo

演示

Alternatively, implement an active class for the content elements, too:

或者,也为内容元素实现一个活动类:

/* make this more specific to avoid coloring content elements */
ul.tab li a:focus, ul.tab li a.active {
    background-color: #ccc;
}

/* add this to the CSS */
.tabcontent.active {
    display: block;
}

/* update the markup to add the class */
<div id="London" class="tabcontent active">