随地点击关闭菜单 [英] Close a menu with an anywhere click
本文介绍了随地点击关闭菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你们可以看到,我有一个下拉菜单。
As you guys can see, I have a drop down menu.
我有很多列,每个列都有一个打开菜单的选项。
I have a lot of columns, each one has an option to open the menu.
$(".optionCont").live("click", function(){
$(".dropMenuCont").slideUp();
if ($(this).next().css("display") == "none") {
$(this).next().slideDown();
} else {
$(this).next().slideUp();
}
});
那么,当我点击页面的任何位置时,如何进行菜单slideUp?
So, how can I do the menu slideUp when I click in any place of the page?
像文档点击一样?
我访问了其他主题,但我不知道为什么,这不起作用。也许我是以不同的方式做的。
I visited others topics, but I don't know why, this is not working. Maybe I'm doing it in a diferent way.
我接受菜单编码中的任何提示。
I accept any tips in the menu coding.
演示: Jsfiddle
推荐答案
在回调中注册一次性处理程序 以确保下一次点击关闭菜单:
Register a one-off handler inside the callback to make sure the next click closes the menu:
$(".optionCont").live("click", function(ev){
$(".dropMenuCont").slideUp();
if($(this).next().css("display") == "none"){
$(this).next().slideDown();
}else{
$(this).next().slideUp();
}
ev.stopPropagation();
$(document).one('click', function() {
$(".dropMenuCont").slideUp();
});
});
请参阅 http ://jsfiddle.net/alnitak/GcxMs/
这篇关于随地点击关闭菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文