防止子触发父点击事件中的点击 [英] Prevent click from child firing parent click event

查看:159
本文介绍了防止子触发父点击事件中的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择器绑定一个点击事件,将删除弹出窗口。但是,我只希望选择器处理点击,而不是选择器的子元素,以便能够触发点击事件。

I have a selector that binds a click event which will remove the popup. However, I only want the selector to handle the click, instead of the children of the selector to be able to fire the click event.

我的代码:

<div id="popup">
  <div class="popup-content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
</div>

点击 .popup-content 它会触发点击事件,当我不想让 #popup 的孩子这样做。

When clicking on .popup-content, it will fire the click event when I don't want the children of #popup to do so.

jQuery代码:

$('#popup').bind('click', function()
{
    $(this).remove();
});


推荐答案

try:

e.stopPropagation();
return false; 

在您的事件处理程序中

这篇关于防止子触发父点击事件中的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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