jQuery:单击功能排除子级. [英] jQuery: click function exclude children.

查看:100
本文介绍了jQuery:单击功能排除子级.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图绕过jQuery".not()"函数,遇到一个问题.我希望父div是可单击的",但是如果用户单击子元素,则不会调用该脚本.

Trying to wrap my head around the jQuery ".not()" function, and running into a problem. I would like to have the parent div to be "clickable" but if a user clicks on a child element, the script is not called.

$(this).not(children()).click(function(){
   $(".example").fadeOut("fast");
});

html:

<div class="example">
   <div>
      <p>This content is not affected by clicks.</p>
   </div>
</div>

推荐答案

为此,请停止在子

这将阻止孩子的点击超过其水平冒泡,从而使父级不会收到点击.

This will stop the child clicks from bubbling up past their level so the parent won't receive the click.

.not()的用法略有不同,它从选择器中过滤掉元素,例如:

.not() is used a bit differently, it filters elements out of your selector, for example:

<div class="bob" id="myID"></div>
<div class="bob"></div>

$(".bob").not("#myID"); //removes the element with myID

对于单击,您的问题是单击子项时,它会冒出与父项的距离,而不是您无意中为孩子添加了一个点击处理程序.

For clicking, your problem is that the click on a child bubbles up to the parent, not that you've inadvertently attached a click handler to the child.

这篇关于jQuery:单击功能排除子级.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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