jQuery:fadeOut只有当父div被点击 [英] jQuery: fadeOut only when parent div is clicked

查看:140
本文介绍了jQuery:fadeOut只有当父div被点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery

$(".window-container").click(function() {
    $(this).fadeOut("fast");
});

HTML

<div class="window-container">
    <div class="window-wrap">
        <div class="window-content">
             asdf
        </div>
    </div>
</div>

我有一个链接,使 window-container 和每个子div出现。 (像Facebook弹出窗口)

I have a link that makes window-container and each child div appear. (Like a Facebook popup)

关闭 window-container 我想能够点击 window-content div。因为如果我有一个输入包含在内,我专注于它,它会关闭一切。

To close window-container I want to be able to click anywhere except within the window-content div. Because if I have an input contained within, and I focus on it, it will close everything.

那么如何让它只有关闭,如果用户点击 window-container window-wrap divs?

So how can I have it only close if a user clicks the window-container or window-wrap divs?

推荐答案

只需检查 活动。目标 ,以确保点击事件源自 div.window-container div.window-wrap

Just check event.target to make sure the click event originated from div.window-container or div.window-wrap.

$(".window-container").click(function(e)
{
    if ($(e.target).is('div.window-container, div.window-wrap'))
    {
        $(this).fadeOut("fast");
    }
});

或者:

$(".window-container").click(function(e)
{
    if ($(e.target).has('div.window-content').length)
    {
        $(this).fadeOut("fast");
    }
});

这篇关于jQuery:fadeOut只有当父div被点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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