Question

I want to pop out a menu, and when the user click positions other than the menu, the menu will hide. Also, if the click is on some nodes who have click listeners, those handling functions will not be triggered. I can use a transparent mask layer to implement this, but is there any easier way (since I use jQuery)? I tried the :not selector

$(document).on('click',':not(.menu)',function(e){
$('.menu').hide();
});

But it seems just not working (also should I add a e.stopPropgation() here)?

Était-ce utile?

La solution

I would write something like this:

$(document).on('click', function(e){
  if($(e.target).parents(".menu").length == 0)
     $('.menu').hide();
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top