jQuery click off element event

后端 未结 15 994
無奈伤痛
無奈伤痛 2020-12-13 00:25

I have a floating div that gets displayed, and I want it to be hidden when the user clicks off the div. This would be similar to the .hover() function callback when hoverin

相关标签:
15条回答
  • 2020-12-13 00:49

    If you do not want to hide the element that you will show by clicking itself:

    var div_active, the_div;
    
    the_div = $("#the-div");
    div_active = false;
    
    $("#show-the-div-button").click(function() {
      if (div_active) {
        the_div.fadeOut(function(){
          div_active = false;
        });
      } else {
        the_div.fadeIn(function(){
          div_active = true;
        });
      }
    });
    
    $("body").click(function() {
      if div_active {
        the_div.fadeOut();
        div_active = false;
      }
    });
    
    the_div.click(function() {
      return false;
    });
    
    0 讨论(0)
  • 2020-12-13 00:50

    example you click a link element to display div menu , you simply bind blur function to link element to hide div menu

    $('a#displaymenu').click(function(){
       $("#divName").toggle();
    }).blur(function() {$("#divName").hide()})
    
    0 讨论(0)
  • 2020-12-13 00:52

    If you're using Jquery, you could use a selector like:

    $("*:not(#myDiv)").live("click", function(){
        $("#myDiv").hide();
    });
    
    0 讨论(0)
提交回复
热议问题