[英]jQuery to hide a DIV when the user clicks outside of it, but not on perticular one div
当用户单击某个div而不是某个特定的div时,我想隐藏一个div
$('.todo_menu_open').click(function(){
$('.css_content').toggle();
$('.todo_before').toggle();
$('.todo_after').toggle();
});
$(document).mouseup(function (e) {
var container = new Array();
container.push($('.css_content'));
container.push($('.todo_before'));
container.push($('.todo_after'));
$.each(container, function(key, value) {
if (!$(value).is(e.target) // if the target of the click isn't the container...
&& $(value).has(e.target).length === 0) // ... nor a descendant of the container
{
$(value).hide();
}
});
});
但不要单击一个container.push($('。css_content'));
希望hasclass会有用。 它返回一个布尔值
if (!$(e.target).hasClass('css_content')){
$(value).hide();
}
您还可以使用id
选择器
if(e.target.id !=="someIdOfContainer){
$(value).hide();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.