简体   繁体   English

如何防止父div在子类被点击时注册点击; event.stopPropagation(); 做有趣的事

[英]How to prevent the parent div from registering the click when a child class is clicked; event.stopPropagation(); doing funny things

We've got the classic problem with a div's child getting clicked and the parent's click event gets triggered as well.我们遇到了一个经典问题,即 div 的子级被点击,而父级的点击事件也被触发。 I've got a button set within a container that expands and unexpands upon clicking.我在容器中设置了一个按钮,单击时可以展开和展开。

The button, when clicked, should:单击该按钮时,应:

  • Unexpand the container展开容器
  • Hide the container's description隐藏容器的描述

The two click functions are given below:下面给出了两个点击函数:

var $NotificationContainer = $("#NotificationContainer");
$NotificationContainer.append('<div class="Notification" title="'+title+'"></div>');
var $thisNotification = $NotificationContainer.children('.Notification[title='+uniqueTitle+']');
$thisNotification.append('<div class="NotificationDescription">'+uniqueDescription+'</div>');
$(".NotificationDescription").hide();

// Button used to close an expanded notification
$thisNotification.append("<div class='NotificationCloseButton'></div>");
$('.NotificationCloseButton').hide();

$thisNotification.click(function()
{
        $(this).animate({height:250}, 1000);
        $(this).find('.NotificationDescription').slideToggle('fast');
        $(this).find('.NotificationCloseButton').slideToggle('fast');
});

$(".NotificationCloseButton").click(function()
{
        $thisNotification.animate({height:50}, 1000);
        $(this).find('.NotificationDescription').slideToggle('fast');
        $(this).find('.NotificationCloseButton').slideToggle('fast');
});

What I find with this code is that when clicking the close button:我用这段代码发现的是,当点击关闭按钮时:

  • SlideToggles the description to be hidden SlideTogles 要隐藏的描述
  • SlideToggles the close button to be hidden SlideToggles 要隐藏的关闭按钮
  • The container unexpands, but then re-expands (contents still hidden)容器未展开,但随后重新展开(内容仍然隐藏)

The $thisNotification click is being called (I think).正在调用$thisNotification点击(我认为)。


Now, when I try to use event.stopPropagation();现在,当我尝试使用event.stopPropagation(); or a simple return false;或简单的return false; in the closeButton's click, I get very interesting results.在 closeButton 的点击中,我得到了非常有趣的结果。

Clicking the close button with either of the above additions now:现在单击带有上述任一添加项的关闭按钮:

  • Unexpands the container展开容器
  • The description and button remain present, and do not slideToggle at all.描述和按钮仍然存在,根本不滑动切换。

Code snippets of the exact way I implemented stopPropogation and return false:我实现 stopPropogation 并返回 false 的确切方式的代码片段:

$(".NotificationCloseButton").click(function(event)
{
    event.stopPropagation();
    $thisNotification.animate({height:50}, 1000);
    $(this).find('.NotificationDescription').slideToggle('fast');
    $(this).find('.NotificationCloseButton').slideToggle('fast');
});

and

$(".NotificationCloseButton").click(function()
{
    $thisNotification.animate({height:50}, 1000);
    $(this).find('.NotificationDescription').slideToggle('fast');
    $(this).find('.NotificationCloseButton').slideToggle('fast');
    return false;
});

You have click bindings for a the parent object:您具有父对象的单击绑定:

$thisNotification

and for a child object:对于子对象:

$(".NotificationCloseButton")

When you click the close button, the 'click' event is being fired for both handlers, all animations are queued, and you get the undesirable closes-then-opens action.当您单击关闭按钮时,两个处理程序都会触发 'click' 事件,所有动画都在排队,并且您会得到不希望的关闭然后打开操作。

You have a few options to resolve this.您有几个选项可以解决此问题。 #1 is to unbind the parent click handler and rebind it after the close button is clicked. #1 是取消绑定父单击处理程序并在单击关闭按钮后重新绑定它。

$thisNotification.click(function()
{
    notificationClickHandler(); //animations are separated from bindings
    $thisNotification.unbind('click');
});

Alternately, jQuery has a .clearQueue() method that removes all queued animations.或者,jQuery 有一个 .clearQueue() 方法可以删除所有排队的动画。 This might have side-effects when users are quick with the mouse, or if your page is heavy on jQuery animations, so you'll have to experiment with the appropriate level of scope for your application.当用户快速使用鼠标时,或者如果您的页面大量使用 jQuery 动画,这可能会产生副作用,因此您必须为您的应用程序尝试适当的范围级别。

$(".NotificationCloseButton").click(function()
{
    $thisNotification.animate({height:50}, 1000);
    $(this).find('.NotificationDescription').slideToggle('fast');
    $(this).find('.NotificationCloseButton').slideToggle('fast');
    $.clearQueue();
});

暂无
暂无

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

相关问题 当 Event.stopPropagation 不起作用时,如何防止在祖先元素上注册相同的事件? - How to prevent the same event registering on ancestor elements when Event.stopPropagation doesn't work? event.stopPropagation(); 隐藏下拉列表,但不是单击它时 - event.stopPropagation(); to hide dropdown, but not when it is clicked 单击子div时如何取消绑定父div的click事件 - how to unbind the click event of parent div when clicked on child div 当孩子被点击时如何防止父div的点击 - How to prevent click of parent div when child is clicked 复选框click事件中的event.stopPropagation是否可以防止IE 8及更早版本中的change事件触发? - Does event.stopPropagation in a checkbox click event prevent the change event from firing in IE8 and earlier? Angular onChange 不会触发 event.stopPropagation 以防止外部 div 的点击 - Angular onChange doesn't trigger event.stopPropagation to prevent outer div's click ng-class $ event.stopPropagation()无法在Angular中工作 - ng-class $event.stopPropagation() not working on parent in angular Javascript-event.stopPropagation()在BS4手风琴上无法从父到子工作 - Javascript - event.stopPropagation() not working from parent to child on BS4 accordion <a>尽管有 $event.stopPropagation,但在标记</a>内部单击时会发生重定向 - Redirect happens when clicked inside of the <a> tag, despite $event.stopPropagation 从子级拖动到父级时如何防止父级单击事件 - How to prevent parent click event when dragging from child to parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM