jQuery文档的点击事件,但忽略div [英] jQuery click event for document but ignore a div

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

问题描述

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

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.

有人可以为整个网页设置点击事件,但是当有人在DIV中点击评论框/保存按钮时,会忽略它?

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?

推荐答案

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

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();
});

如果不起作用,请尝试使用 preventDefault()

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

e.preventDefault();

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

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