The question is probably worded in a very confusing way so here's a quick example / if you click an "Archive", a dropdown will appear. Now if you want to close it, you need to click "Archive" again. However I want to close it by any click that doesn't target "Option 1", "Option 2". Similar to how Facebook does it if you click the "Account" menu in the top right corner.
The only solution that es to my mind is to tie an onClick function to either or if that won't work to all elements present on the page except several elements that I'll specify. This solution looks a little hackish to me so I wonder if there is a smarter solution that I am missing.
The question is probably worded in a very confusing way so here's a quick example http://www.andytimney./projects/jquerydropdown/ if you click an "Archive", a dropdown will appear. Now if you want to close it, you need to click "Archive" again. However I want to close it by any click that doesn't target "Option 1", "Option 2". Similar to how Facebook does it if you click the "Account" menu in the top right corner.
The only solution that es to my mind is to tie an onClick function to either or if that won't work to all elements present on the page except several elements that I'll specify. This solution looks a little hackish to me so I wonder if there is a smarter solution that I am missing.
Share Improve this question edited Apr 13, 2011 at 17:12 Jacob 78.9k24 gold badges157 silver badges241 bronze badges asked Apr 13, 2011 at 17:00 EugeneEugene 4,3377 gold badges39 silver badges54 bronze badges3 Answers
Reset to default 2Read this How do I detect a click outside an element?
Attach an click event to the body and test if the clicked item was not an option:
$('body').click( function(e) {
if(!$(e.target).parents('.option').length && !$(e.target).hasClass('.option')) {
$('.archive').hide();
}
});
have you tried document click and stop propagation for the event when menu click is triggered? interesting article on event bubbling http://www.quirksmode/js/events_order.html
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744843278a4596686.html
评论列表(0条)