简体   繁体   English

Meteor.js:阻止事件传播的最佳方法

[英]Meteor.js : best way to stop event propagation

Consider this template in Meteor.js. 在Meteor.js中考虑此模板。

<template name="trial">
  <li id='{{ _id }}'">{{ name }}
    <span>X</span>
  </li>
</template>

I want to add an event to the "li" and another one to the "span". 我想将一个事件添加到“ li”,将另一个事件添加到“ span”。 How can I prevent the click on the span to fire the event attached to the li? 如何防止单击跨度来触发与li关联的事件? I've found some posts talking about this, but they're years old. 我发现一些有关此的文章,但它们已经有几岁了。

Many thanks for your help! 非常感谢您的帮助!

To prevent event from bubbling-up to parent elements, use stopPropagation() of the event object. 为防止事件冒泡到父元素,请使用事件对象的stopPropagation() Works on either native JS events, or React events: 适用于本机JS事件或React事件:

spanClickHandler(event) {
    event.stopPropagation();

    // handle click
    ...
}

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

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