如何在jQuery中捕获任何点击事件 [英] How to catch any click event in jquery

查看:619
本文介绍了如何在jQuery中捕获任何点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击按钮时,如果有一个按钮,它会显示一个带有图像的div(如聊天的表情面板),如果再次单击该按钮,div会隐藏,但是我想做的是: 如果div已经显示出来,然后单击页面上的其他任何内容,我想将其隐藏.我试过了:

I have a button, when it's clicked, shows a div with images(like an emoticon panel of a chat) if I click it again the div hides, but what I want to do is: If the div is already showed up and then I click any other thing of the page, I want to hide it. I tried this:

$("myBtn").click(function(){
    // show div
});

$(document).click(function(){
// hide div
});

单击"myBtn"时,div会显示并自动隐藏.我该如何解决?

When "myBtn" is clicked, the div shows up and hide automatically. How could I fix it ?

谢谢您的时间.

推荐答案

您可以尝试以下操作:

$(document).on('click', function(evt) {
    if(!$(evt.target).is('#my-id')) {
        //Hide
    }
});

更新

那么您就可以拥有一个完整的工作集:

Just so you can have a full working set:

$('#mybutton').on('click', function(evt) {
    $('#mydiv').show();
    return false;//Returning false prevents the event from continuing up the chain
});

这篇关于如何在jQuery中捕获任何点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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