jquery:如果在div内部,请点击鼠标 [英] jquery: get mouse click if inside a div or not

查看:79
本文介绍了jquery:如果在div内部,请点击鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个HTML页面

<html>
<body>
<div>a</div>
<div>b</div>
<div>c</div>
<div>d</div>
<div id='in_or_out'>e</div>
<div>f</div>
</body>
</html>

a,b,c,d,e和f可以是div而不仅仅是纯文本。

a,b,c,d,e and f could be divs also not just a plain text.

我想获得鼠标点击事件,但我怎么知道它是在#in_or_out div里面还是外面?

I want to get the mouse click event, but how could i know if it's inside or outside #in_or_out div ?

:伙计们,我知道如何检查div是否点击,但我想要在点击超出div时触发我的事件

EDIT :: guys, i know how to check if the div is click or not, but i want my event to be fired when the click is outside that div

推荐答案

$("body > div").click(function() {
    if ($(this).attr("id") == "in_or_out") {
        // inside
    } else {
        // not inside
    }
});

编辑:刚刚得知,有一个否定:

just learned, that there is a negate:

$("body > div:not(#in_or_out)").click(function(e) {
    // not inside
});

这篇关于jquery:如果在div内部,请点击鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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