简体   繁体   English

在div之外单击时隐藏div

[英]hiding div when clicked outside the div

Please look at the code here : http://jsbin.com/esokic/10/edit#source 请在此处查看代码: http : //jsbin.com/esokic/10/edit#source

When I click on customer support a div is shown 当我单击客户支持时,将显示一个div

What I want is when someone clicks out of the div, the div should hide, I tried a couple of things, but they don't seem to work.. 我想要的是,当有人单击div时,该div应该隐藏,我尝试了几件事,但它们似乎不起作用。

$(document.body).one("click", function() {$(".cust-support-outer").hide();
});

Also: 也:

$("body").click(function(e){
    if(e.target.className !== "csupport-drop")
    {
      $(".cust-support-outer").hide();
    }       
});

Would appreciate any help... 希望有帮助...

--Arnab -阿纳布

Try: 尝试:

var mouse_is_inside = false;

$(document).ready(function()
{
    $('.cust-support-outer').hover(function(){ 
        mouse_is_inside=true; 
    }, function(){ 
        mouse_is_inside=false; 
    });

    $("body").mouseup(function(){ 
        if(! mouse_is_inside) $('.cust-support-outer').hide();
    });
});

Bind this to body 绑定到身体

$("body").click(function() {
    if ($(this).attr("class") == "cust-support-outer") {
        // inside
    } else {
        // not inside
    }
});

Arnab I did this change in your js and worked Arnab我在您的js中做了此更改并工作了

try this, use this js code 试试这个,用这个js代码

  $(function(){
    $(".csupport-drop").click(function(){
    $(".csupport-drop").addClass("active-drop-tab");
    $(".cust-support-outer").show();
      return false
    });

  $(document).bind("click", function(e) {  
    if(!$(e.target).hasClass("get-daily-alerts-outer")){
      $(".get-daily-alerts-outer").hide()
    }
  });

  $(".close").click(function(){$(".get-daily-alerts-outer").hide();
   return false
  });
  $(".get-deal-alerts").click(function(){$(".get-daily-alerts-outer").show();
   return false
  });
});

I just changed how you bind the "click" event to the document and pass the Event object to the function so you can check over what element the click event was fire. 我只是更改了将“ click”事件绑定到文档并将Event对象传递给函数的方式,因此您可以检查一下click事件引发了哪些元素。

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

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