未捕获的SyntaxError:其他意外的令牌,难倒

时间:2015-05-25 17:11:25

标签: javascript jquery css toggle

所以我正在制作一个带滑动切换功能的导航菜单。我想要在菜单打开时隐藏切换按钮,但我仍然希望它占据空间,以免弄乱格式化。

我的javascript代码如下:

(function() {
$('.menu-toggle').bind('click',function() {
                $('body').toggleClass('menu-open');
                if ($('body').hasClass('menu-open') == true) {
                    $('.outside-toggle').style.visibility = "hidden";
                else
                    $('.outside-toggle').style.visibility = "visible";
                };
    return false;
});

})();

我尝试了许多不同的语法约定,但似乎都没有。我收到错误" Uncaught SyntaxError:Unexpected token else"

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

更改

if ($('body').hasClass('menu-open') == true) {
    $('.outside-toggle').style.visibility = "hidden";
else
    $('.outside-toggle').style.visibility = "visible";
};

if ($('body').hasClass('menu-open')) { // == true is useless
    $('.outside-toggle')[0].style.visibility = "hidden"; // missing [0]
} else { // <------ HERE THE MISSING BRACES
    $('.outside-toggle')[0].style.visibility = "visible"; // style is a property of a DOM element, not of a jquery object
}

或更好:

$('.outside-toggle').css('visibility', $('body').hasClass('menu-open') ? "hidden" : "visible");