为什么这个.not()选择器不起作用?

kam*_*808 1 html javascript jquery

我有一个侧边栏响应式菜单,单击按钮时会打开.菜单有下拉子菜单,并使用twitter bootstrap模板.

当我点击菜单按钮和菜单本身以外的任何地方时,我试图隐藏菜单.

但点击菜单的例外不起作用.即使单击菜单,它也会隐藏.根据我的问题在于.not()选择器.

这可能有什么问题?

代码是:

CSS

.menu-click
{
    padding: 5px;
    cursor: pointer;
    border: solid 1px #ffcccc;
}

.mobile-menu
{
    top: 50px;
    display: none;
    position: absolute;
    z-index: 999;
    animation-duration:0.5s;
   -o-animation-duration:0.5s;
   -webkit-animation-duration:0.5s;
   -moz-animation-duration:0.5s;
}
Run Code Online (Sandbox Code Playgroud)

JQuery的

$(function(){

    $("html").not('.mobile-menu').click(function(){
        $(".mobile-menu").removeClass("animated fadeInLeft").addClass("animated fadeOutLeft")
    });
    $(".menu-click").click(function(event){
        event.stopPropagation();
    });
});
Run Code Online (Sandbox Code Playgroud)

Dan*_*mms 6

jQuery.not过滤您正在调用它的匹配元素.什么在你的问题的代码所要求的是所有<html>那些元素不是 .mobile-menu元素.

你可能想要这样的东西:

$("ul").not('.mobile-menu').click(...)
Run Code Online (Sandbox Code Playgroud)

在评论中讨论之后,如果你有一个divwith .mobile-menu,并且你想要检查它.mobile-menu是否没有,你需要以div另一种方式识别,例如添加一个.menu类.

<div class="menu">...</div>
<div class="menu mobile-menu">...</div>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您可以选择非.mobile-menu一个:

$('.menu').not('.mobile-menu').click(...)
Run Code Online (Sandbox Code Playgroud)

不添加.menu类和使用$('.div').not('.mobile-menu')会选择div页面上没有的每一个.mobile-menu,而不仅仅是你感兴趣的那个.