简体   繁体   English

jQuery:在这种情况下,.on()的正确用法是什么?

[英]jQuery: what correct use of .on() in this situation?

Consider the following markup: 考虑以下标记:

 <div class="parent">
     <div class="child">
         button
    </div>
 </div>

I need to run a function on a click event on the child class and I've got the following two options: 我需要在child类的click事件上运行一个函数,并且有以下两个选项:

 $(".parent").on("click", ".child", function(){....});

and

 $(document).on("click", ".child", function(){....});

Is there a dramatic difference on performance between using a direct parent as a target and the document itself? 使用直接parent对象作为目标与document本身之间在性能上有显着差异吗? To me using document seems as a more robust option (if the parent class was changed for instance) - just need to make sure it won't cause problems if I start using this method everywhere. 对我来说,使用document似乎是一个更可靠的选择(例如,如果更改了父类)-仅需确保如果我开始在各处使用此方法,就不会引起问题。

PS The child is added dynamically inside the parent hence I'm using .on() PS child被动态添加到parent内部,因此我正在使用.on()

$(".parent").on("click", ".child", function(){....});

binds your click event to only those elements with class '.parent' that are present within the document and the event is bubbled from the target('.child') to the element where the handler is attached. 将您的click事件仅绑定到文档中存在的类“ .parent”的元素,并且该事件从target('。child')冒泡到附加了处理程序的元素。 This is much preferable than adding it to document as, adding it to document like 这比将其添加到文档中,将其添加到文档中这样更可取

$(document).on("click", ".child", function(){....});
  1. Bubbles the click event from the target in the document where they occur all the way up to the body and the document element. 使单击事件从文档中的目标起泡,一直发生到正文和文档元素。

  2. As @Woff mentioned, bound handlers wont get removed on removal of .parent elements. 如@Woff所述,绑定处理程序不会在删除.parent元素时被删除。

"Performance" should not be an issue in your case. 在您的情况下,“性能”应该不是问题。

There is an obvious difference between your two choices : 您的两个选择之间有明显的区别:

 <div class="parent">
     <div class="child" id="A">
         button
     </div>
 </div>
 <div class="child" id="B">
     button
 </div>

If you bind your handler on document , clicking on #B will trigger the handler. 如果将处理程序绑定到document ,则单击#B将触发该处理程序。
If you bind your handler on .parent , clicking on #B won't trigger the handler. 如果将处理程序绑定到.parent ,则单击#B不会触发该处理程序。

You have to decide if what you want is to listen to all nodes tagged with the .child class in the document, or only the ones which are nested under a .parent element. 您必须决定是要监听文档中所有带有.child类标记的节点,还是只.parent嵌套在.parent元素下的.parent


Other points to take into account : 要考虑的其他要点:

  • IMHO it is best to narrow as much as possible the scope of your actions. 恕我直言,最好是尽可能缩小您的行动范围。 From your question, $('.parent') seems to be the best target. 从您的问题来看, $('.parent')似乎是最好的目标。

  • However, if your .parent elements are also dynamically added / removed, do not use them as binding targets. 但是,如果.parent元素也被动态添加/删除,请不要将它们用作绑定目标。 Try to target some other node which you have created, but which will always remain in the DOM (for example : wrap your html in a #myModule div and bind your events to $('#myModule') ). 尝试以您创建的其他节点为目标,但这些节点将始终保留在DOM中(例如:将html包裹在#myModule div中,并将事件绑定到$('#myModule') )。

  • .stopPropagation() : the main difference in delegation vs. direct binding is that the handler is executed when the event has bubbled up to the target node. .stopPropagation() :委托与直接绑定的主要区别在于,当事件冒泡到目标节点时,将执行处理程序。
    If you rely on .stopPropagation() or return false to prevent the execution of other handlers, this has an impact on when the event is actually stopped. 如果您依靠.stopPropagation()return false来阻止其他处理程序的执行,则这会影响事件实际停止的时间。
    Here is a fiddle to illustrate this. 这是一个小提琴来说明这一点。

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

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