单击页面上其他任何内容时隐藏元素 [英] hiding an element on click of anything else on the page

查看:78
本文介绍了单击页面上其他任何内容时隐藏元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提及Franek的问题这里我还有一个问题.

referring to Franek's question found here I have one more question.

上面的链接上的解决方案对我来说一直有效,直到我在页面上引入了另一个菜单.在这种情况下,有两个菜单彼此相邻.当我单击其中之一时,将显示相关的div,其中显示了可能的选项可供选择.然后,当我单击文档时,div关闭.但是,当我单击任何其他元素时,它仍然显示.

The solution on the link above worked for me until I introduced another menu to my page. In this case, there are two menus next to each other. When I click on one of them, the relevant div is displayed showing possible options to select. Then, when I click on the document the div gets closed. But when I click on any other element it is still displayed.

对我来说,一个解决方案是运行代码以关闭其他任何元素单击以及文档单击上的菜单.

A solution for me would be to run the code to close the menu on any other element click as well as document click.

我该如何实现?

(菜单:不可见的div元素,单击其标题即可显示)

(menu: invisible div element that when clicked on its title becomes visible)

推荐答案

这稍微好一点,因为它还会检查被单击元素的父元素:

This is slightly better, as it also check the parent(s) of the element being clicked:

$(document).click(function(e) {
    var target = e.target;

    if (!$(target).is('#menu') && !$(target).parents().is('#menu')) {
        $('#menu').hide();
    }
});

这篇关于单击页面上其他任何内容时隐藏元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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