简体   繁体   English

在外部单击时隐藏div

[英]hide the div when clicking outside

I have a hidden div which opens up on click of a link text. 我有一个隐藏的div,单击链接文本即可打开。 I need to hide the div and remove the active class from the link text when user clicks outside the div anywhere on the body. 我需要隐藏div并在用户在主体外部的div之外单击时从链接文本中删除active类。

I have used body onclick to hide the div but it is hiding the div on div click as well. 我使用body onclick来隐藏div,但它也在div click上隐藏了div。 I don't want to close the div only on body click, not on the div click. 我不想仅在单击主体时关闭div,而不是在单击div时关闭。 How can I stop the action on div click? 如何停止div点击操作?

here is what I have tried 这是我尝试过的

$('.link').click(function(e){
e.stopPropagation();
    $(this).toggleClass('active');
    $(".box").slideToggle();
});

$('body').click(function(e) {
    $(".box").hide();
        $(".link").removeClass('active');
});

DEMO DEMO

Try to use event.target jQuery. 尝试使用event.target jQuery。

event.target event.target

 $('body').click(function(event) { if (!$('.box').is(event.target) && $('.box').has(event.target).length === 0) { $('.box').slideUp(); $('.link').removeClass('active'); } }); $('.link').click(function(event) { event.stopPropagation() $(this).toggleClass('active'); $(".box").slideToggle(); }); 
 .header { background: #fff; padding: 20px } body { height: 600px; font: 13px Verdana; } .link_div { position: relative } .link { display: inline-block; outline: none; padding: 10px; color: black; background: #e1e1e1; box-shadow: -1px -3px 3px 0px rgba(109, 109, 109, 0.2); } .box { display: none; background: #8bdeff; padding: 30px; position: absolute; top: 40px; left: 0 } .active { background: #8bdeff } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="header"> <div class="link_div"> <a href="#" class="link">Open box</a> <div class="box"> I am a box </div> </div> </div> 

Here's what I recommend you do: 这是我建议您执行的操作:

$('.link').click(function(e){
    e.stopPropagation();
    $(this).toggleClass('active');
    $(".box").slideToggle();
});

$('body').click(function(e) {
    var target = e.target;

    // Short-circuit if box is clicked OR
    // link is not active
    if ($(target).is('.box')) return;
    if (!$(".link").hasClass('active')) return

    $(".box").slideToggle();
    $(".link").removeClass('active');
});

Some things to note: 注意事项:

  1. e is the event passed to your eventListener when clicked, it has a target that you can check for. e是单击后传递给eventListener的事件,它具有可以检查的目标。
  2. I'm short-circuiting when the box is the target clicked or if the link is already active 当框是单击目标或链接已激活时,我正在短路
  3. I'm using the slideToggle instead of the hide because it's nicer, and since this will only trigger when the link is active, you don't have to worry about clicking outside the div and having the link toggle/open when it's closed. 我使用的是slideToggle而不是hide,因为它更好,并且仅当链接处于活动状态时才会触发,因此您不必担心在div外部单击并在链接关闭时使链接切换/打开。

Hope that helps! 希望有帮助!

JSFiddle: https://jsfiddle.net/Lm85m7nb/ JSFiddle: https ://jsfiddle.net/Lm85m7nb/

You can just check if the clicked element is the class .box and do nothing if it is. 您可以仅检查clicked元素是否为.box类,如果是,则什么也不做。

 $('.link, .box').click(function(e) { // Added .box to the click-function if ($(this).attr("class") == "box") { // check if the clicked element is .box return false; // do nothing like return false } e.stopPropagation(); $(this).toggleClass('active'); $(".box").slideToggle(); }); $('body').click(function(e) { $(".box").hide(); $(".link").removeClass('active'); }); 
 .header { background: #fff; padding: 20px } .link_div { position: relative } .link { display: inline-block; outline: none; padding: 10px; color: black; background: #e1e1e1; box-shadow: -1px -3px 3px 0px rgba(109, 109, 109, 0.2); } .box { display: none; background: #8bdeff; padding: 30px; position: absolute; top: 40px; left: 0 } .active { background: #8bdeff } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="header"> <div class="link_div"> <a href="#" class="link">Open box</a> <div class="box"> I am a box </div> </div> </div> 

Hi You can use this code for add document click event for click to close in outside 嗨,您可以使用此代码添加文档单击事件,以在外部单击以关闭

$('.link').click(function(e){
  e.stopPropagation();
  e.preventDefault();
    $(this).toggleClass('active');
    $(".box").slideToggle();
});

$(document).click(function(e) {
    $(".box").hide();
        $(".link").removeClass('active');
})

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

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