JavascriptjQuery: how to catch a click that targets any element except for several specified elements? - Stack Overflow

The question is probably worded in a very confusing way so here's a quick exampleif you click a

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 badges
Add a ment  | 

3 Answers 3

Reset to default 2

Read 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信