当点击任何div之外的空白空间时,在jsquery中触发事件 [英] Trigger event in jsquery when clicking on empty space, outside of any div

查看:310
本文介绍了当点击任何div之外的空白空间时,在jsquery中触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图隐藏一个div,当1)某些其他div被点击时,和2)页面边缘的空白被点击,空间不包含任何div。我已经覆盖了第一部分,但我不知道如何做的第二部分。我不想让div隐藏,当任何外面的div被点击。

I'm trying to hide a div both when 1) certain other divs are clicked, and when 2) empty space on the margins of the page are clicked, space not contained by any div. I've got the first part covered, but I do not know how to do the second. I don't want the div to hide when ANYTHING outside the div is clicked. Just when empty space is clicked.

以下是一个简单的 jsfiddle 来玩。

Here's a simple jsfiddle to play with.

这里是我使用的基本jsquery。

And here's the basic jsquery I'm using. Can I add something that will make "text1" or "text2" disappear when the user clicks empty space?

$("#2").click(function() {
   $('#text1').slideUp(300);
});

$("#1").click(function() {
   $('#text2').slideUp(300);
});


推荐答案

将被点击并执行此操作:

You can simply add a specific class to the div items that will be clicked and do this:

$(document).click(function (event) {//when anywhere is clicked on the page    
    if (!$(event.target).hasClass('MyClass')) {//if the clicked item has this class
        $('#text2').slideUp(300);//hide     
        $('#text1').slideUp(300);//hide      
    }      
});

DEMO: https://jsfiddle.net/tjrsvbuc/3/

这篇关于当点击任何div之外的空白空间时,在jsquery中触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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