单击外部后关闭div菜单 [英] Close a div menu after clicking outside it

查看:115
本文介绍了单击外部后关闭div菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个HTML代码:

I have the next HTML code:

// Container for link where users will click to open the second div

<div class="topbar-block topbar-profile-options">
   <a class="open-popup-on-click" data-popup-id="topbar-user-actions"></a>
</div>

// Container-popup

<div id="topbar-user-actions" class="hidden popup"></div>



点击此处查看完整代码(Github项目)



和jQuery代码(它是's通用电器neralized代码,使用类打开 - 弹出 - 点击的所有链接:


Click here to see full code (Github project).

And the jQuery code (it''s a generalized code, to work with all links with class open-popup-on-click):

$('.open-popup-on-click').unbind('click').click(function() {
    $('#' + $(this).data('popup-id')).toggleClass('hidden');
    return false;
  });



点击这里查看完整代码(Github项目)



现在我想推广这个jQuery代码,同时关闭单击弹出窗口外的菜单。



我一直在寻找答案两天,但我不能得到一个jQuery代码可以按我的意愿工作(打开/以正常方式关闭,并在点击外部弹出窗口时隐藏。



请有人帮帮我吗?对我来说最重要的是理解你的代码,而不只是得到答案。



提前致谢。


Click here to see full code (Github project).

Now I want to generalize this jQuery code, to also close the menus when clicking outside the popup.

I have been looking for an answer two days, but I can''t get a jQuery code to work as I want (open/close on normal way, and hide when clicking outside popup).

Please, could someone help me? The most important for me is to understand your code, not just getting an answer.

Thanks in advance.

推荐答案

' 。open-popup-on-click')。unbind(' 单击。单击( function (){
('.open-popup-on-click').unbind('click').click(function() {


' #' +


this )。data(' popup-id'))。toggleClass(' hidden');
返回 false ;
});
(this).data('popup-id')).toggleClass('hidden'); return false; });



点击此处查看完整版代码(Github项目)



现在我想概括一下这个jQuery代码,也可以在弹出窗口外单击时关闭菜单。



我一直在寻找答案两天,但我不能得到一个jQuery代码可以按照我想要的方式工作(以正常方式打开/关闭,并在外部点击时隐藏弹出)。



拜托,有人可以帮帮我吗?对我来说最重要的是理解你的代码,而不仅仅是得到答案。



提前致谢。


Click here to see full code (Github project).

Now I want to generalize this jQuery code, to also close the menus when clicking outside the popup.

I have been looking for an answer two days, but I can''t get a jQuery code to work as I want (open/close on normal way, and hide when clicking outside popup).

Please, could someone help me? The most important for me is to understand your code, not just getting an answer.

Thanks in advance.


这篇关于单击外部后关闭div菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆