我的移动网站上有那个按钮;问题是,我需要添加一种方法,当用户在菜单外单击或点击时,菜单会关闭。
有人可以指导我吗?
最佳答案
fiddle 链接:http://jsfiddle.net/eAGjM/
您需要检查点击的部分是否既不是菜单也不是它的子元素。 如果您的菜单包含子元素,则需要进行此检查,否则单击子元素也会隐藏菜单。
$(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();
}
});
关于jquery - 移动菜单 - 点击外部菜单关闭菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23333577/