根据条件排除点击事件? [英] Exclude click event based on condition?

查看:128
本文介绍了根据条件排除点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为jQuery事件设置条件。我有一个输入元素和一个div。我想在页面上的任何地方检测到点击并执行该方法,但只有当点击不在输入或div时。

I'm trying to set conditions on a jQuery event. I have an input element and a div. I want to detect a click anywhere on the page and to execute that method but only if the click is not on the input or div.

推荐答案

使用jQuery click()或live()函数,然后使用jQuery is()函数检查点击事件的目标。

Use the jQuery click() or live() functions and then check the target of the click event using the jQuery is() function.


  1. 绑定点击事件文件

  2. 如果目标没有输入或div继续

$(document.body).click(function(e) {
  if( !$(e.target).is("input, div") ) {
    console.log("Run function because image or div were not clicked!");
  }
});

示例网页=> http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-document-click-exclusion.html

在示例页面上点击之后的示例firebug输出

Example firebug output after clicking around on example page

这篇关于根据条件排除点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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