简体   繁体   English

我的下拉菜单无法正常工作

[英]I can't get my drop down menu to work

I want for people to hide/show nav when they click "Menu". 我想让人们在单击“菜单”时隐藏/显示nav Here is my code: 这是我的代码:

<script>
    document.querySelector(".switch").onclick = function() {
        if (document.querySelector(".mobileNav ul").className == "drop"){
            this.className = "dropShow";
        }
        else {
            this.className = "drop";
        }
    };

</script>

but it doesn't do anything. 但它什么也没做。

Almost there. 快好了。 Just a minor error. 只是一个小错误。

this.className assigns the className to the button element with class .switch . this.className使用class .switch将className分配给按钮元素。 Instead do the following: 而是执行以下操作:

document.querySelector(".switch").onclick = function() {

    var ulElem = document.querySelector(".mobileNav ul");

    if ( ulElem.className== "drop"){
        ulElem.className = "dropShow";
    }
    else {
        ulElem.className = "drop";
    }
};

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

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