How to hide a DIV element when I click outside

2019-03-12 20:18发布

I have a div and want to hide it when I click outside. My code is:

<div id="mydiv">The div must be above button</div>

    $('#mydiv').click(function(e) {
        e.stopPropagation();
    });

    $(document).click(function() {
        $('#mydiv').fadeOut(300);
    });

But it is not working for me ...

UPDATE

Full code is presented below. When I click on a button it shows a div above, so I need to hide this div when I click outside.

DEMO

<div id="but" style="text-align: right;"><button type="button">Show Div!</button></div>
<div id="mydiv" style="display:none;">The div must be above button</div>

$("#but button").click(function(){
  var pos = $(this).offset(),
      div = $("#mydiv");

  // Make it visible off-page so
  // we can measure it
  div.css({
    "display": "block",
    "border": "1px solid black",
    "position": "absolute",
    "left": -10000,
    "top": 0
  });

  // Move it where we want it to be
  div.css({
    "left": pos.left - 40,
    "top":  pos.top - div.height() - 10
  });
});

$('#myDiv').click(function(e) {
    e.stopPropagation();
});
$(document).click(function() {
    $('#mydiv').fadeOut(300);
});

标签: jquery html hide
9条回答
我命由我不由天
2楼-- · 2019-03-12 20:45

Try the below solution which is easy and it's even working recursive:

$(document).mouseup(function(e) 
{
    var container = $("YOUR CONTAINER SELECTOR");

    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) 
    {
        container.hide();
    }
});
查看更多
对你真心纯属浪费
3楼-- · 2019-03-12 20:51

The simplest way is to capture the onclick event of the document. You know this click event happens outside the element that you are interested, then just return false. I used this trick to hide a popup menu when a click outside the menu event fires.

查看更多
Ridiculous、
4楼-- · 2019-03-12 20:52

Better than binding click event to document, you can use this kind of snippet:

SEE DEMO

$('#mydiv').click(function(e) {
        e.stopPropagation();
    })
    .css({outline:0})
    .focusout(function(){
         $(this).fadeOut(300);  
    }).focus();
查看更多
Fickle 薄情
5楼-- · 2019-03-12 20:53

In javascript click is a bubbling event, it starts on a div and goes up to a document. When you stop an event propagation using a stopPropagation() function, a click on the document is not handled. So, to solve your problem just remove e.stopPropagation().

DEMO 1

The best way is:

$('#mydiv').click(function(e) {
    e.stopPropagation();
    $(this).fadeOut(300);
});

DEMO 2

If I click on this div, how to make to click outside and hide this div ?

Ok, let's imagine, that when you are clicking on a container with id "wrapperId", "myDiv" should be hidden:

$("#wrapperId").click(function(e){
  $('#mydiv').fadeOut(300);
})

if container is a document, you can use $(document) instead of $("#wrapperId").

DEMO 3

It is not working look what is happening: jsbin.com/ilowij/7

You should stop a click event propagation when you are clicking the button. Make these changes:

$("#but button").click(function(e){
    e.stopPropagation();
    ...
}

DEMO 4

查看更多
叼着烟拽天下
6楼-- · 2019-03-12 20:54

try .blur(), and like this:

$('#mydiv').blur(function(e) {
    $(this).fadeOut(300);
});

Hope it help

查看更多
仙女界的扛把子
7楼-- · 2019-03-12 20:57

check this one

<a href="#" id="link">Click me</a>
show or hide me by clicking

    (function($) {

  $("#link").click(function(e){
    e.stopPropagation();
    var div = $("#show_hide");

    // Make it visible off-page
    div.css({
      "display": "block"
    });

  });
$(document).click(function(e){
  $('#show_hide').fadeOut(300);
});
})(jQuery);

you can checkout this link for example..

check this http://jsfiddle.net/x5Env/

查看更多
登录 后发表回答