我无法让我的下拉菜单工作

时间:2022-03-23 19:39:01

I want for people to hide/show nav when they click "Menu". 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.

但它没有做任何事情。

1 个解决方案

#1


0  

Almost there. Just a minor error.

差不多了。只是一个小错误。

this.className assigns the className to the button element with class .switch. Instead do the following:

this.className使用类.switch将className分配给button元素。而是执行以下操作:

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

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

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

#1


0  

Almost there. Just a minor error.

差不多了。只是一个小错误。

this.className assigns the className to the button element with class .switch. Instead do the following:

this.className使用类.switch将className分配给button元素。而是执行以下操作:

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

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

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