我有这个html/css代码:

<body>
        <!-- BEGIN: HEADER AREA -->
        <?php require("snippets/header_area.php"); ?>
        <!-- END: HEADER AREA -->
        <div id = "presentation_area">
            <div id = "submenu_area">
                <div id = "sliding_menu_area">
                </div>
                <div id = "tags">
                    <div id = "1">
                        <img src = "lang/el/images/1.png" />
                    </div>
                    <div id = "2">
                        <img src = "lang/el/images/2.png" />
                    </div>
                    <div id = "3">
                        <img src = "lang/el/images/3.png" />
                    </div>
                    <div id = "4">
                        <img src = "lang/el/images/4.png" />
                    </div>
                </div>
            </div>
        </div>
    </body>

当用户单击#sub_menu_area以外的其他所有内容时,我试图触发一个函数。我使用js的以下部分:
$('*').not('#submenu_area').click(function(){
        if(slide == "open"){
            $('#sliding_menu_area').toggle(effect);
            slide = "close";
        }
    });

不幸的是, child 并没有以某种方式被排除在外,并且在单击时切换了很多次。有人知道怎么做吗? :)

编辑:
因此,我需要一个可以正确获取的jquery选择器:on click of all elements of body EXCEPT for #submenu_area and its descendants

最佳答案

试试这个:

$(document).click(function(e) {
        if ($(e.target).is('#submenu_area, #submenu_area *')) {
            return;
        }
        if(slide == "open"){
            $('#sliding_menu_area').toggle(effect);
            slide = "close";
        }
    });

10-04 21:31