移动菜单-单击外部菜单以关闭菜单 [英] Mobile Menu - Click outside menu to close menu

查看:98
本文介绍了移动菜单-单击外部菜单以关闭菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的移动网站上有该按钮;问题是,我需要添加一种方法,当用户在菜单外部单击或点击时,菜单将关闭.

I have that button in my mobile website; the problem is, that I need to add a method that when the user clicks or taps outside the menu the menu closes.

有人可以指引我吗?

推荐答案

小提琴链接: http://jsfiddle .net/eAGjM/

Fiddle Link : http://jsfiddle.net/eAGjM/

您需要检查单击的部分是否既不是Menu也不是其子元素. 如果您的菜单包含子元素,则需要进行此检查,否则单击子元素也会隐藏菜单.

You'll need to check if the clicked portion is neither the Menu nor its child element. If your menu contains child element then this check is required otherwise clicking on sub elements will also hide the menu.

$(document).mouseup(function(e){
   var menu = $('selector');
   if (!menu.is(e.target) // The target of the click isn't the container.
   && menu.has(e.target).length === 0) // Nor a child element of the container
   {
      menu.hide();
   }
});

这篇关于移动菜单-单击外部菜单以关闭菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆