简体   繁体   English

使用event.stopPropagation()

[英]Use of event.stopPropagation()

In the case of a game where user have to click/use keys mutli and many times, 在用户必须多次点击/使用密钥的游戏的情况下,

Is there any advantage to use event.stopPropagation() in all event handler which i know bubbling is useless? 在所有事件处理程序中使用event.stopPropagation()是否有任何优势,我知道冒泡是没用的?

Suppose i have a div with a click event binded, 假设我有一个带有点击事件绑定的div,

<div id="mydiv">CLICK</div>
$('#mydiv').click(function(){...});

if i click on it, this event will propagate to all ancestors elements of this div (eg body/html/document/window). 如果我点击它,这个事件将传播到这个div的所有祖先元素(例如body / html / document / window)。 So just wondering if this could be better or same or worst to stop immediatly propagation of event. 所以只是想知道这是否可能更好,相同或更差,以阻止事件的立即传播。

In fact, i don't know how javascript engine internally deal with events binded (or not) to elements, if this works as a listener or what... 事实上,我不知道javascript引擎如何在内部处理绑定(或不绑定)元素的事件,如果它作为一个监听器或什么...

It is a bit general, but to answer your direct question, unless you need to stop propagation then there is no reason to do so. 这有点笼统,但要回答你的直接问题,除非你需要停止传播,否则没有理由这样做。 Propagation is not what is going to hinder the efficiency of your game, it's having too many event bindings. 传播并不会妨碍你的游戏效率,它有太多的事件绑定。 Consider using a generic binding on a parent element and then processing the actual target of the event to determine what action to take. 考虑在父元素上使用通用绑定,然后处理事件的实际目标以确定要采取的操作。

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

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