单击此按钮时

<div id="header-nav-container">
            <div id="explore" class="btn btn-clear header-nav">
                <a title="Explore">Explore </a>
            </div>


我希望打开此菜单,然后单击agin关闭,如切换

 <div id="header-menus">
 <div id="explore-menu" class="header-menu open">


html - 单击按钮时,通过使用javascript向类(class)添加一个单词来添加/删除菜单的[toggle]-LMLPHP

最佳答案

尝试这个:

<a title="Explore" onclick="toggleOpen();">Explore </a>

function toggleOpen(){
    if (document.getElementById("explore-menu").className == "header-menu") {
        document.getElementById("explore-menu").className = "header-menu open";
    }
    else {
        document.getElementById("explore-menu").className = "header-menu";
    }
}

10-08 12:53