Javascript切换菜单打开但不关闭

时间:2018-06-26 18:49:26

标签: javascript

我正在学习JS,并且制作了可折叠的导航栏。单击按钮后,我打开了菜单,但不会关闭。我已经在该站点上搜索了答案,但是发现的所有内容都涉及Bootstrap。这只是纯JS。我还花了几个小时检查代码,以发现语法错误或任何可能出错的地方。任何帮助将不胜感激!

这是我的代码:

let toggleNavStatus = false;

let toggleNav = function() {
let getSidebar = document.querySelector(".nav-sidebar");
let getSidebarUl = document.querySelector(".nav-sidebar ul");
let getSidebarTitle = document.querySelector(".nav-sidebar span");
let getSidebarLinks = document.querySelectorAll(".nav-sidebar a");

if (toggleNavStatus === false) {
    getSidebarUl.style.visibility = "visible";
    getSidebar.style.width = "315px";
    getSidebarTitle.style.opacity = "0.5";

    let arrayLength = getSidebarLinks.length;
    for (i = 0; i < array.length; i++) {
        getSidebarLinks[i].style.opacity = "1";
    }

    toggleNavStatus = true;

 }

else if (toggleNavStatus === true) {
     getSidebar.style.width = "50px";
     getSidebarTitle.style.opacity = "0";

    let arrayLength = getSidebarLinks.length;
    for (i = 0; i < array.length; i++) {
        getSidebarLinks[i].style.opacity = "0";
    }

    getSidebarUl.style.visibility = "hidden";

    toggleNavStatus = false;
}

}

1 个答案:

答案 0 :(得分:0)

在for循环中,您需要将array.length更改为arrayLength

查看此codepen