简体   繁体   English

jQuery单击文档的事件但忽略div

[英]jQuery click event for document but ignore a div

I have a online Slide Show I'm working on using jQuery. 我有一个在线幻灯片我正在使用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. 我使用$(document).click事件来检测用户何时点击页面以了解何时显示幻灯片中的下一个项目符号点或移动到下一页。

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? 有没有办法可以为整个页面设置click事件,但是当有人点击DIV时,忽略它,注释框/保存按钮在哪里?

You will most likely need to stop the propagation of events in your Comments div using the event object's stopPropagation() method: 您很可能需要使用事件对象的stopPropagation()方法停止在Comments div中传播事件:

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

if that doesn't work try using preventDefault() : 如果这不起作用尝试使用preventDefault()

e.preventDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM