简体   繁体   English

单击按钮后更改选项卡

[英]Changing Tabs upon Button Click

Currently, I am using the guide at the following link to determine how to build tabs ( http://www.w3schools.com/howto/howto_js_tabs.asp ). 目前,我正在使用以下链接中的指南来确定如何构建选项卡( http://www.w3schools.com/howto/howto_js_tabs.asp )。

     function openTab(evt, tabName) {
        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(tabName).style.display = "block";
        evt.currentTarget.className += " active";
     }

     // Get the element with id="defaultOpen" and click on it
     document.getElementById("defaultOpen").click();

I want to change the tab that is displayed upon pressing a button. 我想更改按下按钮时显示的选项卡。 Therefore, in the eventListener code in javascript: 因此,在javascript中的eventListener代码中:

     document.getElementById("btn_click").addEventListener("click", function() {
             openTab("click", "tab2");
     }

This gives me that "click" as evt is undefined. 这使我无法确定evt的“点击”。 Would appreciate any help in how to define the parameter evt to change tabs upon pressing the button (a javascript soln would be preferable). 希望获得任何帮助,以帮助您定义如何在按下按钮时定义参数evt来更改选项卡(最好使用javascript soln)。

You as it says on the w3schools website, you just have to run this code when the button is pressed. 正如您在w3schools网站上所说的那样,您只需要在按下按钮时运行此代码即可。

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";
}

Here is both the HTML and JS: http://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_tabs 这是HTML和JS: http : //www.w3schools.com/howto/tryit.asp? filename= tryhow_js_tabs

Hope this helps! 希望这可以帮助!

You need to pass the actual click event like so: 您需要像这样传递实际的click事件:

document.getElementById("btn_click").addEventListener("click", function(e){
    openTab(e, "tab2");
})

Also as a general rule: try to avoid w3schools are the plague. 同样作为一般规则:尽量避免出现瘟疫。 Quality is overall very low. 质量总体很低。 Better bet is to just search stackoverflow for solutions to specific questions, and use mozilla for reference to all things DOM-related. 更好的选择是只在stackoverflow中搜索特定问题的解决方案,并使用mozilla来引用与DOM相关的所有事物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM