如何使用Javascript默认显示第一个标签?

时间:2016-04-08 16:19:49

标签: javascript

我想在每次加载页面时默认显示TAB1

脚本:

Sys.setenv(JAVA_HOME="C:\\\PATH\\\TO\\\JAVA\\\")
library(rJava)

我只需要tab1来显示加载我该如何做到这一点?

2 个答案:

答案 0 :(得分:1)

脚本

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()。

答案 1 :(得分:0)

您可以添加在c​​lick事件中添加的相同规则,但仅添加到数组的第一个元素。这样的东西应该工作(假设你想要显示的元素是数组中的第一个元素):

<script>
    // Get all elements with class="tabcontent" into an array
    tabcontent = document.getElementsByClassName("tabcontent");

    // display the first element of the array
    tabcontent[0].style.display = "block";

    // Get all elements with class="tablinks" into an array
    tablinks = document.getElementsByClassName("tablinks");

    // make first element active
    tablinks[0].classList.add("active");
</script>

并在HTML页面的末尾添加代码