[英]jquery click outside - $(this) stop works
I wanted to ask, where i have error, it piece of code. 我想问一下,哪里有错误,它是一段代码。
Button class show / hide filter element, and this is ok. 按钮类显示/隐藏过滤器元素,这没关系。 But i want to archive "click outside effect" - close filter on some body element click, not filter itself. 但是我想存档“外部点击效果”-在某些身体元素点击上关闭过滤器,而不是过滤器本身。
$('.button').on('click',function() {
var filters = $('#filters');
if (filters.is(':hidden')) {
filters.show();
} else {
filters.hide();
}
// Now i want to close filter, on click on body element, not filter itself.
// Now, this code below, closes filter, on click on body element, but now .button is not closing filter. So code below is not working...
$(document).mouseup(function (e) {
var container = filters;
if (!container.is(e.target) && container.has(e.target).length === 0) {
container.hide();
}
});
});
But now, $(document).mouseup(function (e) - it prevents .button, from filters close.... 但是现在,$(document).mouseup(function(e)-它可以防止.button过滤器关闭。
What's wrong ? 怎么了 ?
Thanks for advice. 谢谢你的建议。
Have a look at this: 看看这个:
https://jsfiddle.net/t1nxwvy2/3/ https://jsfiddle.net/t1nxwvy2/3/
You can use the following to see if the click was inside the button or not: 您可以使用以下命令查看单击是否在按钮内部:
$(document).mouseup(function (e) {
var container = $('.button');
if (!container.is(e.target) && (container.has(e.target).length === 0)) {
if (!filters.is(e.target) && (filters.has(e.target).length === 0)) {
filters.hide();
}
} else filters.toggle();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.