[英]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');
});
Try to use event.target
jQuery. 尝试使用event.target
jQuery。
$('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: 注意事项:
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.