Javascript 文档单击以隐藏菜单
当我在菜单外单击文档时,我的文档单击功能没有隐藏菜单。当我点击Javascript 文档单击以隐藏菜单,javascript,jquery,Javascript,Jquery,当我在菜单外单击文档时,我的文档单击功能没有隐藏菜单。当我点击img时,它会显示菜单,当我再次点击img时,它会隐藏它,但当我点击文档时,我希望它隐藏菜单,有人知道我做错了什么以及如何使它工作吗 var visible = false; var id = $(this).attr('id'); $(document).not('#' + id + ' div:eq(1)').click(function () { if (visible) { $
img
时,它会显示菜单,当我再次点击img
时,它会隐藏它,但当我点击文档时,我希望它隐藏菜单,有人知道我做错了什么以及如何使它工作吗
var visible = false;
var id = $(this).attr('id');
$(document).not('#' + id + ' div:eq(1)').click(function () {
if (visible) {
$('.dropdownlist .menu').hide();
visible = false;
}
});
$(this).find('div:eq(1)').click(function (e) {
var menu = $(this).parent().find('.menu');
if (!visible) {
menu.show();
visible = true;
} else if (visible) {
menu.hide();
visible = false;
}
menu.css({ 'left': $(this).position().left + $(this).width() - menu.find('ul').width(),
'top': $(this).position().top + $(this).height() });
})
我遇到了一个类似的问题,并用以下代码解决了它:
$("body").mouseup(function(){
if (visible) {
$('.dropdownlist .menu').hide();
visible = false;
}
});
而不是您的
$(文档)。not(…
代码)。我遇到了类似的问题,并用以下代码解决了它:
$("body").mouseup(function(){
if (visible) {
$('.dropdownlist .menu').hide();
visible = false;
}
});
//add event.stopPropagation() when the user clicks on a .menu element
$('.menu').on('click', function (event) {
//.stopPropagation() will stop the event from bubbling up to the document
event.stopPropagation();
});
//add the click event handler to the image that will open/close .menu elements
$('img').on('click', function (event) {
//we call .stopPropagation() again here so the document won't receive this event
event.stopPropagation();
//cache .menu element
var $div = $('.menu');
//this if statement determines if the .menu should be shown or hidden, in my example I'm animating its top property
if ($div.css('top') == '-50px') {
$div.stop().animate({top : 0}, 250);
} else {
$div.stop().animate({top : '-50px'}, 150);
}
});
//add click event handler to the document to close the .menu
$(document).on('click', function () {
$('div').stop().animate({top : '-50px'}, 150);
});
而不是您的$(文档)。而不是(..
代码)
//add event.stopPropagation() when the user clicks on a .menu element
$('.menu').on('click', function (event) {
//.stopPropagation() will stop the event from bubbling up to the document
event.stopPropagation();
});
//add the click event handler to the image that will open/close .menu elements
$('img').on('click', function (event) {
//we call .stopPropagation() again here so the document won't receive this event
event.stopPropagation();
//cache .menu element
var $div = $('.menu');
//this if statement determines if the .menu should be shown or hidden, in my example I'm animating its top property
if ($div.css('top') == '-50px') {
$div.stop().animate({top : 0}, 250);
} else {
$div.stop().animate({top : '-50px'}, 150);
}
});
//add click event handler to the document to close the .menu
$(document).on('click', function () {
$('div').stop().animate({top : '-50px'}, 150);
});
jsfiddle:
JSFIDLE:有趣的解决方案,非常有启发性。我个人使用全局变量contextMenu,其中包含对jquery对象中包装的活动contextMenu的引用,因为这不仅告诉我上下文菜单需要隐藏,还告诉我哪个菜单。有趣的解决方案,非常有启发性。我个人使用全局变量contexttMenu,其中包含对包装在jquery对象中的活动contextMenu的引用,因为这不仅告诉我上下文菜单需要隐藏,还告诉我哪个菜单。与其使用bool指示上下文菜单是否可见,不如使用对上下文菜单对象的引用。如上所述使用它,还可以告诉您哪个菜单需要隐藏,哪个是当有多个上下文菜单时非常方便。这不会干扰菜单项上的单击事件。使用对上下文菜单对象的引用,而不是指示上下文菜单是否可见的布尔值。如上所述使用它,但它也会告诉您哪些菜单需要隐藏,这在有多个上下文菜单时非常方便。确实如此不要干扰菜单项上的单击事件。