简体   繁体   中英

Javascript toggle element display

I have a website with a menu element in a header grid and when the page is first displayd the menu is visible. I have a hamburger element which I want to use to toggle the menu display when it is clicked.

/*==================================================================*/
/* NAVIGATION */
.main-nav {
    grid-area: N;
    text-transform: uppercase;
}

/* Style the nav links */
.main-nav ul {
    display: grid;
    grid-template-columns: repeat(4,20vw);
}

.main-nav li {
    /* No bullets */
    text-align: center;
    list-style-type: none;
    min-width: 20vw;
}

.main-nav a {
    /* Not italic unlike all other a elements */
    text-decoration: none;
    font-style: normal;
}

In the end it was a simple error of leaving out .style as per Simon K's comment:

if (menu.style.display == "none"){
        alert("Hidden");
        menu.style.display = "grid";
    }
    else {
        alert("Displayed");
        menu.style.display = "none";
    }

Try this instead!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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