如何在导航栏中自动启用选项卡?

时间:2016-07-26 09:42:58

标签: javascript html css

下面是导航栏的html,css和Javascript代码。这里只有当我们点击各自的图标(即伦敦,巴黎,东京)时才显示每个导航栏的内容,但我想要的内容每当加载页面时,首先显示的第一个导航栏。可以对以下代码进行哪些更改?        HTML代码:          

<head>
<style>
#footer {
    background-color:black;
    color:white;
    clear:both;
    text-align:center;
    padding:5px;
     width:100%;
     align:center;

}

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

ul.tab li {float: left;}

ul.tab li a {
    display: inline-block;
    color: black;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

ul.tab li a:hover {
    background-color: #ddd;
}

ul.tab li a:focus, .active {
    background-color: #ccc;
}

.tabcontent {
    display: none;
    padding: 6px 12px;
    -webkit-animation: fadeEffect 1s;
    animation: fadeEffect 1s;
    padding-left: 5%;
}

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

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

</style>

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

<body>
<center>
<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>
</center>
<br>
<br>
<div id="London" class="tabcontent">
  <h3>London</h3>
  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>
<div id="footer">
© Cities
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

请在创建新问题之前进行搜索。 你可以让你的第一个tabcontent显示。可以是内联html或css:

.tabcontent:nth-of-type(1){display:block;}