简体   繁体   English

如何从子元素中删除数据目标和数据切换或禁止元素触发事件?

[英]How to remove data-target and data-toggle from child elements or disable elements from triggering the event?

HTML: HTML:

<div id="card_{{_id}}" class="card-panel" data-toggle="modal" data-target="#{{_id}}">
      <span class="white-text">{{text}}</span>
      <div class="card-action">
        {{#each tags}}
        <div class="chip">
          <tag class="tag">{{this}}</tag>
          <i id="removeTag" class="material-icons fa fa-ban"></i>
        </div>
        {{/each}}
        <div class="chip" id="likeButton">
          <i class="fa fa-thumbs-o-up"></i>&nbsp;{{likes}}&nbsp;&nbsp; Likes
        </div>
      </div>
    </div>

above is sample html code from my meteor project. 以上是来自我的meteor项目的示例html代码。 i want the div#card element to activate the modal by clicking on it. 我希望div#card元素通过单击来激活modal But i do not want the div.chip elements to toggle the modal when clicked. 但我不希望div.chip元素在单击时切换modal Is there a way i can disable child element from the data-toggle of the modal? 有没有办法可以从模态的data-toggle中禁用子元素?

If you can use javascript, you can do stopPropagation for stopping click event on div.chip from bubbling to div#card . 如果你可以使用javascript,你可以做stopPropagation来停止div.chip上的点击事件从冒泡到div#card div.chip

$('.chip').on('click', function (ev) {
    ev.stopPropagation();
});

For elements in a meteor template, you do something like this (replace yourTemplate with your template name), 对于流星模板中的元素,您可以执行以下操作(将模板替换为您的模板名称),

Template.yourTemplate.events({
    'click .div', function (ev, template) {
        ev.stopPropagation();
    }
});

See the JSFiddle JSFiddle

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

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