将jQuery click事件分配给除了几个div及其子项之外的正文中的所有内容 [英] Assign jQuery click event to the everything in the body except a few divs and their children

查看:75
本文介绍了将jQuery click事件分配给除了几个div及其子项之外的正文中的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在页面上按div时,会出现一个弹出窗口。再次单击div时,弹出窗口消失。当你点击div的外面时,弹出窗口消失 - 到目前为止看起来都很好。

When I press a div on my page, a popup appears. When you click the div again, the popup disappears. When you click outside of the div, the popup disappears - all looking good so far.

问题是,当我点击弹出窗口时,我想要它以便弹出窗口它的子项是可点击的(它们是无序列表中的链接)。但是,我似乎无法让它发挥作用。这是我的代码

The problem is, when I click the popup, I want it so that the popup and it's children are clickable (they're links inside an unordered list). But, I can't seem to get that to work. Here is my code

        $(function () {
            $('#messageNotification').click(function (event) {
            $("body").one('click', function () {

                jQuery(".userInfo").fadeOut();
            });
            event.stopPropagation();
        });


          <body>
            <div id="messageNotification"><div class="notif same">0</div></div>
            <div id="messages">
                <ul class="userInfo" style="">
                    <li></li>
                </ul>
            </div>

          </body>


推荐答案

如果我理解正确,您可以按选择器进行过滤。例如:

If I'm understanding you correctly, you can filter by selector. For example:

$('body').on('click', 'a', function(){ ... });

Thi s会将点击事件绑定到与选择器 a 匹配的所有元素。

This would bind the click event to all elements matching the selector a.

如果你想将点击绑定到与弹出窗口相匹配的所有元素而不是,你可以这样做:

If you want to bind the click to all elements not matching your popup, you could do something like:

$('body').on('click', ':not(#popup)', function(){ // code to dismiss popup here });

请参阅:not() .on()了解更多信息......

See the jquery documentation for :not() and .on() for more info...

这篇关于将jQuery click事件分配给除了几个div及其子项之外的正文中的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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