所以我正在制作一个带滑动切换功能的导航菜单。我想要在菜单打开时隐藏切换按钮,但我仍然希望它占据空间,以免弄乱格式化。
我的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"
任何帮助都将不胜感激。
答案 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");