简体   繁体   English

JS事件鼓舞了DOM

[英]JS Event bubbling up the DOM

On my meteor app I have comments and within those comments I have a button "reply" when I click on reply the form to leave an additional comment will open. 在我的流星应用程序上,我有评论,在这些评论中,当我单击“答复”以留下其他评论时,将有一个“答复”按钮将打开。

The problem is that when I click on a reply button it opens the form on all the other comments as well as the one clicked instead of only where I clicked. 问题是,当我单击答复按钮时,它会打开所有其他注释以及单击的注释的表单,而不仅仅是在单击的地方。

this is my code 这是我的代码

template.replyComments.events({
    'click #replyToCommentButton2': function(e) {
         $(".commentToShow").show();
    },
    //...
)};

and html 和html

  <button class="btn waves-effect waves-light"  data-show="#form2"  id="replyToCommentButton2">Reply
    <i class="mdi-content-send right"></i>
  </button>

Try using stopPropagation and narrowing down the .commentToShow class, maybe adding a data attr to the button you are clicking that will tell you the element to show: 尝试使用stopPropagation并缩小.commentToShow类的范围,也许在单击的按钮上添加一个数据attr,它将告诉您要显示的元素:

HTML: HTML:

<a id="replyToCommentButton2" data-show="#form2">Reply</a>

JS: JS:

template.replyComments.events({
'click #replyToCommentButton2': function(e) {
         e.stopPropagation();
         $(this).parent().find('.commentToShow').show();

},

)};

There are two things you'll need to do, one of them is to stop the propagation event from bubbling up the event chain and the second one is only reference the current object (referenced with this), because you're targeting all the classes instead of the current object. 您需要做两件事,其中之一是阻止传播事件冒泡事件链,其二是仅引用当前对象(对此进行了引用),因为您要定位所有类而不是当前对象。

template.replyComments.events({
'click #replyToCommentButton2': function(e) {
    e.stopPropagation();
    var targetedForm = $(this).data("show");
    $(".commentToShow", targetedForm).show();

},

This should help to understand event propagation: What's the difference between event.stopPropagation and event.preventDefault? 这应该有助于理解事件传播: event.stopPropagation和event.preventDefault之间有什么区别?

So this is not really answer to your question, but unless you are using some third party lib that requires you to manipulate the DOM with jQuery there is a more Meteoric approach to this. 因此,这并不是真正解决您的问题的方法,但是除非您使用某个第三方库,而该库要求您使用jQuery操作DOM,否则将有一种更多的流星方法。

// comment.coffee
Template.comment.created = ->
  # Keep the state of the comment in a ReactiveVar on the template instance
  this.showReplyField = new ReactiveVar

Template.comment.events
  "click [data-reply]": (e, tmpl) ->
    e.stopPropagation()
    # If the reply field is open then close it and vice verse
    currentState = tmpl.showReplyField.get()
    tmpl.showReplyField.set !currentState

Template.comment.helpers
  reply: ->
    # Get the state and expose it to the template. It will update reactively when the value changes and only for this template/comment.
    Template.instance().showReplyField.get()

// comment.html
<template name="comment">
    <p>{{text}} </p>
    {{#if reply}}
      {{> newComment}}
    {{else}}
      <button class="button" data-reply>reply</button>
    {{/if}}
</template>

<template name="newComment">
  <!-- Your new comment html here -->
</template>

Note that you will need to meteor add reactive-var to add the reactive var package. 注意,您需要meteor add reactive-var来添加react var包。

This is what I'm using in my comments package for a blog. 这就是我在博客评论包中使用的内容。 You can check out the source code here if you wish. 您可以根据需要在此处签出源代码

And if you dont like coffeescript you can translate it . 如果您不喜欢coffeescript,可以将其翻译

If you still prefer to use jQuery you should probably use the template optimised version that ships with Meteor. 如果您仍然喜欢使用jQuery,则可能应该使用Meteor随附的模板优化版本。 Within an event handler it's available as tmpl.$ and will only select elements within the template (so children will be included too). 在事件处理程序中,它可以作为tmpl.$并且只会选择模板中的元素(因此也会包括子元素)。

This was the answer to the problem I was having (answered by someone on Meteor forums) 这就是我遇到的问题的答案(在Meteor论坛上有人回答)

template.replyComments.events({
    'click #replyToCommentButton2': function(e, template) {
         template.$(".commentToShow").show();
    },
    //...
)};

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

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