简体   繁体   English

有人可以解释一下这个 stopPropagation 是如何工作的吗?

[英]can someone explain how this stopPropagation works?

I was trying to setup this "when you click outside of the element, close it" type of thing using some code I found on Stackoverflow:我试图使用我在 Stackoverflow 上找到的一些代码来设置这种“当你点击元素外部时,关闭它”类型的东西:

$(document).click(function() {
 $('.list-to-hide').hide();
});

$('.show-list-button').click(function(event) {
 event.stopPropagation();
});

Could someone explain the later part with stopPropagation?有人可以用 stopPropagation 解释后面的部分吗? I don't understand why it's needed.我不明白为什么需要它。

Thanks!谢谢! Matt马特

Imagine this:想象一下:

<div>
    DIV
    <span>
        Span
    </span>
<div>

and:和:

$('div').click(function() { alert('div clicked'); });
$('span').click(function() { alert('span clicked'); });

Check out what happens when you click each one看看当你点击每一个时会发生什么

When you click the span, it happens to also trigger the div because your also clicking the div.当您单击跨度时,它恰好也触发了 div,因为您也单击了 div。

Now if we wanted to alert the span only we need to stop the div click from triggering when we click on the span so we do this:现在,如果我们只想提醒 span,我们需要在点击 span 时停止触发 div 点击,所以我们这样做:

$('div').click(function() { alert('div clicked'); });
$('span').click(function(e) { alert('span clicked'); e.stopPropagation(); });

See what happens now看看现在会发生什么

Your example code is missing a vital part:您的示例代码缺少一个重要部分:

$(document).click(function() {
    $('.list-to-hide').hide();
});

$('.show-list-button').click(function(event) {
    event.stopPropagation();
    $('.list-to-hide').show();
});

Without the event.stopPropagation() , it would show the list, and then hide it because the .show-list-button is inside the $(document) so both click handlers would fire.如果没有event.stopPropagation() ,它将显示列表,然后将其隐藏,因为.show-list-button$(document)内,因此两个单击处理程序都会触发。 event.stopPropagation() basically says only apply this click event to THIS CHILD NODE and don't tell the parent containers anything because I don't want them to react . event.stopPropagation()基本上说只将此单击事件应用于THIS CHILD NODE并且不告诉父容器任何内容,因为我不希望它们做出反应

Think about it this way - you rent a taxi for $100.可以这样想——你以 100 美元租一辆出租车。 The driver gives his company $80.司机给他的公司 80 美元。 event.stopPropagation() is like telling him to keep all $100 because the company doesn't need to know anything about the ride. event.stopPropagation()就像告诉他保留所有 100 美元,因为公司不需要知道任何关于骑行的信息。

event.stopPropagation(); prevents the event from bubbling up the DOM.防止事件冒泡 DOM。 Without this line, clicking on .show-list-button the click handler for document will fire also.如果没有这一行,单击.show-list-button文档的单击处理程序也会触发。 With it, the document click will not fire.有了它,文档点击将不会触发。

Have you read this?你读过这个吗?

http://api.jquery.com/event.stopPropagation/ http://api.jquery.com/event.stopPropagation/

It prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.它防止事件在 DOM 树中冒泡,防止任何父处理程序收到事件通知。

Example例子

Kill the bubbling on the click event.杀死点击事件的冒泡。

$("p").click(function(event){
  event.stopPropagation();
  // do something
}); 

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

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