javascript - jQuery 点击 div 外面应该隐藏 div,点击的子 div 不应该隐藏

标签 javascript jquery html css

我有这样的 HTML 标记

<div class="outside">
    <div class="wrapper">
        <div class="content-wrap">
            <div class="content">
                <div class="content-container">
                    <div class="text">
                        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

我希望当鼠标在 content-wrap div 的外部单击时,content-wrap div 应该被隐藏。在我的标记中,您可以看到我在 content-wrap 之前使用了两个名为 outside 和 wrapper 的 div。但实际上我在内容换行之前有很多 div。如果点击了 content-wrap 的任何子 div,则不应隐藏该 div。那么有人可以告诉我该怎么做吗?任何帮助和建议都将非常感激。 这是 fiddle link

最佳答案

您可以在 .content-wrap 的点击事件上调用 stopPropagation() 来防止 Action 冒泡:

$('div.content-wrap').click(function(e) {
    e.stopPropagation()
});

$("body").click(function(){
   $(".content-wrap").fadeOut(); 
});

FIDDLE

关于javascript - jQuery 点击 div 外面应该隐藏 div,点击的子 div 不应该隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27936411/

相关文章:

javascript - 如何只删除对象的值?

javascript - 带选项的 Stripe 自定义结帐

javascript - 我试图为 Object 设计一个 length() 方法的原型(prototype)并破坏了 jQuery——怎么做到的?

javascript - 从包含中文和拉丁字符的电子邮件字段中检索原始值

javascript - 单击时隐藏/显示 div

javascript - 缺少正则表达式以捕获最后一个键值列表条目

javascript - 如何解码 Javascript/jQuery [object Object]

javascript - 如何设置 ID 并在 mvc 4 razor 中动态使用它?

javascript - 从下拉菜单中获取值后,我可以将其与字符串进行比较吗?

javascript - 如果渲染器进程关闭, Electron 全局变量垃圾会被收集吗?