javascript - jQuery/JS 获取导航菜单以在单击正文时隐藏

标签 javascript jquery html css

我试图让滑出式导航菜单 (menuRight) 在单击菜单本身以外的任何内容时消失。

这是我的 JS:

var menuLeft = document.getElementById( 'cbp-spmenu-s1' ),
        menuRight = document.getElementById( 'cbp-spmenu-s2' ),
        menuTop = document.getElementById( 'cbp-spmenu-s3' ),
        menuBottom = document.getElementById( 'cbp-spmenu-s4' ),
        // showLeft = document.getElementById( 'showLeft' ),
        showRight = document.getElementById( 'showRight' ),
        showTop = document.getElementById( 'showTop' ),
        showBottom = document.getElementById( 'showBottom' ),
        showLeftPush = document.getElementById( 'showLeftPush' ),
        showRightPush = document.getElementById( 'showRightPush' ),
        body = document.body;

        var a=document.getElementsByClassName('showLeft');
        for(var i=0;i<a.length;i++) {
            a[i].onclick = function() {
            classie.toggle( this, 'active' );
            classie.toggle( menuLeft, 'cbp-spmenu-open' );
            disableOther( 'showLeft' );
};

}

showLeft.onclick = function() {
    classie.toggle( this, 'active' );
    classie.toggle( menuLeft, 'cbp-spmenu-open' );
    disableOther( 'showLeft' );
};
showRight.onclick = function() {
    classie.toggle( this, 'active' );
    classie.toggle( menuRight, 'cbp-spmenu-open' );
    disableOther( 'showRight' );
};
showTop.onclick = function() {
    classie.toggle( this, 'active' );
    classie.toggle( menuTop, 'cbp-spmenu-open' );
    disableOther( 'showTop' );
};
showBottom.onclick = function() {
    classie.toggle( this, 'active' );
    classie.toggle( menuBottom, 'cbp-spmenu-open' );
    disableOther( 'showBottom' );
};
showLeftPush.onclick = function() {
    classie.toggle( this, 'active' );
    classie.toggle( body, 'cbp-spmenu-push-toright' );
    classie.toggle( menuLeft, 'cbp-spmenu-open' );
    disableOther( 'showLeftPush' );
};
showRightPush.onclick = function() {
    classie.toggle( this, 'active' );
    classie.toggle( body, 'cbp-spmenu-push-toleft' );
    classie.toggle( menuRight, 'cbp-spmenu-open' );
    disableOther( 'showRightPush' );
};

function disableOther( button ) {
    if( button !== 'showLeft' ) {
        classie.toggle( showLeft, 'disabled' );
    }
    if( button !== 'showRight' ) {
        classie.toggle( showRight, 'disabled' );
    }
    if( button !== 'showTop' ) {
        classie.toggle( showTop, 'disabled' );
    }
    if( button !== 'showBottom' ) {
        classie.toggle( showBottom, 'disabled' );
    }
    if( button !== 'showLeftPush' ) {
        classie.toggle( showLeftPush, 'disabled' );
    }
    if( button !== 'showRightPush' ) {
        classie.toggle( showRightPush, 'disabled' );
    }
}

这里的这部分是我尝试使用的 JQuery 代码(但它不起作用)。

$(":not(#menuRight)").onclick = function() {
    disableOther( 'showLeft' );
}

谢谢!

最佳答案

使用事件传播来观察文档上的点击事件(不在您的菜单中)。

$(document).on('click', function(event) {
  if (!$(event.target).closest('#menucontainer').length) {
    // Hide the menus.
  }
});

关于javascript - jQuery/JS 获取导航菜单以在单击正文时隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28416394/

相关文章:

javascript - Ember.Instrumentation API 示例

javascript - MVC & JS : Using one form in a view and make it look like it's 2 views

html - Sidebar、Navigation 和Content 怎么做呢?

html - 不使用 head 标签的内联样式

javascript - 浏览器调整大小 + slideToggle 不能很好地协同工作

javascript - 使用 jQuery 交换选择列表中的值

javascript - JQuery .replaceWith() 突然将新元素放在错误的位置?

javascript - 事件函数问题

php - 加载用户在网页上选择的图像

javascript - 基础导航顶部栏不适用于 thymeleaf