jQuery单击事件为document但忽略一个div [英] jQuery click event for document but ignore a div

查看:92
本文介绍了jQuery单击事件为document但忽略一个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在线幻灯片我正在使用jQuery。我使用$(document).click事件来检测用户何时点击页面,以了解何时显示幻灯片中的下一个项目符号或移动到下一页。



我遇到的问题是我的工作让我在页面底部插入一个注释框,当有人点击评论框或保存注释按钮时,它也会触发页面的点击事件。



有没有办法可以为整个页面点击事件,但是当有人点击DIV中的评论框/保存按钮时,忽略它?

解决方案

您很可能需要使用事件对象的 stopPropagation() 方法:



($)$ {code $ $''''''''''''

如果不能尝试使用 preventDefault()

  e.preventDefault(); 


I have a online Slide Show I'm working on using jQuery. I have use $(document).click event to detect when a user clicks on the page to know when to show the next bullet point in the slide or to move to the next page.

The problem I'm running into is my job had me insert a comment box on the bottom of the page and when ever someone clicks on the comment box or the save comment button it also fires the click event for the page.

Is there a way I can have the click event for the entire page but ignore it when someone clicks in the DIV the Comment box/Save button are in?

解决方案

You will most likely need to stop the propagation of events in your Comments div using the event object's stopPropagation() method:

$('#comments').click(function(e) {
  e.stopPropagation();
});

if that doesn't work try using preventDefault():

e.preventDefault();

这篇关于jQuery单击事件为document但忽略一个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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