简体   繁体   English

删除元素时如何防止jQuery事件冒泡?

[英]How to prevent jQuery event from bubbling when removing element?

According to jQuery documentation, the event.stopPropagation(); 根据jQuery文档, event.stopPropagation(); should stop the event bubbling. 应该停止事件冒泡。 In the following script, when I remove event.stopPropagation(); 在以下脚本中,当我删除event.stopPropagation(); the function removes the ul element code block including it's all children. 该函数将删除ul元素代码块,包括所有子元素。 I added event.stopPropagation(); 我添加了event.stopPropagation(); to stop bubbling but It returns following error: 停止冒泡,但返回以下错误:

TypeError: undefined is not an object (evaluating 'event.stopPropagation') TypeError:未定义不是对象(正在评估“ event.stopPropagation”)

The following is the HTML and jQuery code: 以下是HTML和jQuery代码:

 $('span.remove').on('click', function() { $(this).parent().hide(500, function(event) { event.stopPropagation(); $(this).parent().remove(); }); }); 
 .todo-list { list-style: none; padding: 0px; } .todo-item { border: 2px solid #444; margin-top: -2px; padding: 10px; cursor: pointer; display: block; background-color: #ffffff; } .remove { float: right; font-family: Helvetica, Arial, sans-serif; font-size: 0.8em; color: #dd0000; } .remove:before { content: 'X'; } .remove:hover { color: #ffffff; } .todo-item::after:hover { background-color: #dd0000; color: white; } .todo-item:hover { background-color: #0EB0FF; color: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class='todo-list'> <li class='todo-item'>4L 2% Milk <span class='remove'></span> </li> <li class='todo-item'>Butter, Unsalted <span class='remove'></span> </li> <li class='todo-item'>Dozen Eggs <span class='remove'></span> </li> <li class='todo-item'>Walk the dog <span class='remove'></span> </li> <li class='todo-item'>Cut the lawn <span class='remove'></span> </li> <li class='todo-item'>Laundry <span class='remove'></span> </li> </ul> 

References: 参考文献:

  1. How to delete parent element using jQuery? 如何使用jQuery删除父元素?
  2. How to stop events bubbling in jQuery? 如何停止jQuery中冒泡的事件?

You have to get your event object from the click() callback. 您必须从click()回调中获取event对象。 If you look at the jquery hide() api docs , the complete callback doesn't get passed anything so your event is undefined, but the click() handler on the other hand gets passed the event object. 如果您查看jquery hide() api docs ,则不会传递完整的回调,因此您的事件是未定义的,但另一方面click()处理函数会通过事件对象。 So you just need to change your javascript to: 因此,您只需要将JavaScript更改为:

$('span.remove').on('click', function(event) {
  event.stopPropagation();
  $(this).parent().hide(500, function() {
    $(this).parent().remove();
  });
});

The reason however that the whole list gets removed is another issue. 但是,整个列表被删除的原因是另一个问题。 You reference parent() twice targeting the ul to be removed, so just get rid of one of the parents (the scope of $(this) changes to the parent in the hide callback). 您两次引用parent()定位要删除的ul,因此只需摆脱其中一个父对象( $(this)的范围将在hide回调中更改为父对象)。 With this edit the code is: 通过此编辑,代码为:

$('span.remove').on('click', function(event) {
  event.stopPropagation();
  $(this).parent().hide(500, function() {
    $(this).remove();
  });
});

It's true however that the propagation of the event, doesn't really have anything to do with the ul being hidden as a whole so you can just remove the event.stopPropagation() line. 但是,确实如此,事件的传播与ul作为一个整体被隐藏并没有任何关系,因此您只需删除event.stopPropagation()行即可。 Final code being: 最终代码为:

$('span.remove').on('click', function() {
  $(this).parent().hide(500, function() {
    $(this).remove();
  });
});

Move event obj to the delegated click event. 将事件obj移至委派的click事件。

Instead of this use event.target . 取而代之的this使用event.target

 $('span.remove').on('click', function(event) { var tgt = event.target; $(tgt).parent().hide(500, function() { $(tgt).parent().remove(); }); }); 
 .todo-list { list-style: none; padding: 0px; } .todo-item { border: 2px solid #444; margin-top: -2px; padding: 10px; cursor: pointer; display: block; background-color: #ffffff; } .remove { float: right; font-family: Helvetica, Arial, sans-serif; font-size: 0.8em; color: #dd0000; } .remove:before { content: 'X'; } .remove:hover { color: #ffffff; } .todo-item::after:hover { background-color: #dd0000; color: white; } .todo-item:hover { background-color: #0EB0FF; color: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class='todo-list'> <li class='todo-item'>4L 2% Milk <span class='remove'></span> </li> <li class='todo-item'>Butter, Unsalted <span class='remove'></span> </li> <li class='todo-item'>Dozen Eggs <span class='remove'></span> </li> <li class='todo-item'>Walk the dog <span class='remove'></span> </li> <li class='todo-item'>Cut the lawn <span class='remove'></span> </li> <li class='todo-item'>Laundry <span class='remove'></span> </li> </ul> 

Multiple problems: 多个问题:

  1. The event variable is not being passed . 未传递event变量 Don't include the event parameter in the hide() function, but rather in the on() event handler. 不要在hide()函数中包含event参数,而应在on()事件处理程序中包含该event参数。 This should stop the error. 这应该停止该错误。
  2. You are removing the parent of the parent. 您要删除父级的父级。 Replace the following line: $(this).parent().remove(); 替换以下行: $(this).parent().remove(); with $(this).remove(); $(this).remove();
  3. You actually don't need the stopPropagation() . 您实际上不需要stopPropagation() After fixing the above two problems, you don't need it. 解决以上两个问题后,您就不需要它了。 The click event is not propagating. click事件没有传播。

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

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