Best way to do it: not in jQuery?

I have a menu in jQuery, when you click on the link that it opens, but I want it to be, when you click somewhere else, anywhere, and not in the menu, it becomes hidden.

I am currently binding the click event to

$(':not(#the_menu)')

But it looks like I'm attaching a click event to everything minus the menu, is there a more efficient way to do something like this?

+5
source share
3 answers

The best way to do this is by capturing bubbles, for example:

$(document).click(function() {
   //close menu
})

$("#the_menu").click(function(e) {
  e.stopPropagation();
});

, ( , return false; event.stopPopagation()), DOM... , . , ... , . 2 1 , , :)

+7

($(body)). #the_menu, :

$(document.body).click(function() {
     //close menu if opened
});

$("#the_menu").click(function(e) {
     //code heere

     e.stopPropagation();
});
0

, ?

$("#parentId").hover(
    function() { //!! on hovering in
        $("#targetId").attr("display","block") ;
    } ,
    function() { //!! on hovering out
        $("#targetId").attr("display","none") ;
    }
) ;

, .

0

All Articles