简体   繁体   English

单击div时隐藏div

[英]Hiding a div when clicking out of it

I added code to make a div, #pending-friend-list-dropdown , close when clicking outside of it. 我添加了代码以创建div, #pending-friend-list-dropdown ,在其外部单击时会关闭。 This works fine, but now when clicking on my image div, friend-icon , the drop-down div will not close now. 这可以正常工作,但是现在当单击我的图像div时, friend-icon ,下拉div现在不会关闭。

As you can see in my snippet, the image div is what opens the drop-down box. 如您在我的代码段中所见,image div是打开下拉框的地方。 I am just trying to figure out how that image div can be used to open and close the drop-down, while using the mouseup function to close the drop-down div as well. 我只是想弄清楚该图像div如何用于打开和关闭下拉菜单,同时也使用mouseup函数也可以关闭下拉div。

 //Hiding Pending Friend Drop-down when clicking out of it $(document).mouseup(function (e) { var container = $("#pending-friend-list-dropdown"); var friend_icon = $("#friend-icon"); if (!container.is(e.target) // if the target of the click isn't the container... && container.has(e.target).length === 0) // ... nor a descendant of the container { container.hide(); } else if (friend_icon.has(e.target)) { container.hide(); } }); //Toggle Pending Friend List $("#friend-icon").click(function() { $('#pending-friend-list-dropdown').toggle(100); }); 
 #main-bar { width: 85%; height: 60px; position: relative; margin-left: 15%; background: red; padding: 3px 0; } #main-bar-container { border: 1px solid black; margin: 0 10px; position: relative; width: 95%; height: 56px; left: 2%; } /*---- Pending Friends List----*/ #friend-icon { display: inline-block; cursor: pointer; position: absolute; right: 20%; top: 15px; } #friend-icon img { height: 30px; width: 30px; } #pending-friend-list-dropdown { height: 500px; width: 400px; overflow: scroll; z-index: 100000; position: absolute; left: 70%; top: 70px; background: blue; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="main-bar"> <div id="main-bar-container"> <div id="friend-icon"><img src="../icons/collection/social.png" alt="Pending Friends"></div> </div> </div> <div id="pending-friend-list-dropdown"> </div> 

You can achieve this more simply by running the code whenever someone clicks on the html-element (the entire page). 只要有人单击html元素(整个页面),就可以运行代码,从而更轻松地实现这一目标。 Then check if the click is located on certain elements. 然后检查单击是否位于某些元素上。

There is also no need to give the instructions in two places for what to be done when clicking on "#friend-icon". 单击“#friend-icon”时,也无需在两个位置给出操作说明。 I have removed the second instance of this in the below code, and just moved the .toggle up to the if statement. 我在下面的代码中删除了该代码的第二个实例,并将.toggle移至if语句。

It now works like a charm: 现在,它就像一种魅力:

$("html").click(function(event) 
{

var container = "#pending-friend-list-dropdown";
var friend_icon = '#friend-icon, #friend-icon img';

    if ( $(event.target).is(friend_icon) ) // clicking on the toggler-div or the img it contains
    {    
        $(container).toggle(100);
    }
    else if (!$(event.target).is(friend_icon) // clicking outside of the toggler
    && !$(event.target).is(container)) // and outside of the toggled div itself
    {
        $(container).hide();   
    }
});

Here's a jsfiddle: https://jsfiddle.net/r54ardcz/2/ 这是一个jsfiddle: https ://jsfiddle.net/r54ardcz/2/

well you can do one thing .. hide div when it loses focus 好吧,你可以做一件事..当它失去焦点时隐藏div

to achieve this make div focusable with tabindex property and then attach onblur event handler 要实现此目标,可以使用tabindex属性使div具有焦点,然后附加onblur事件处理程序

code http://jsbin.com/viginezape/edit?html,css,js,console,output 代码http://jsbin.com/viginezape/edit?html,css,js,控制台,输出

I'll give a third option just so that all the ones I know are on this site. 我将给出第三个选择,以便使我所知道的所有信息都在此站点上。 This is the option that Office Fabric UI uses ( https://dev.office.com/fabric#/components/contextualmenu ) where I think @zheer-mchammer-husain's answer is more along the Twitter Bootstrap model. 这是Office Fabric UI使用的选项( https://dev.office.com/fabric#/components/contextualmenu ),我认为@ zheer-mchammer-husain的答案更多是基于Twitter Bootstrap模型。

Essentially you create a layer over your whole page (height: 100vh and width: 100vw; position: fixed) and then put your dropdown content inside that layer. 本质上,您需要在整个页面上创建一个图层(高度:100vh,宽度:100vw;位置:固定),然后将下拉内容放入该图层中。 When the user clicks that layer, it closes the whole layer at once disappears and all is done. 当用户单击该层时,它将关闭整个层,一次消失,所有操作都完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM