简体   繁体   English

PrototypeJS 中的委托()

[英]delegate() in PrototypeJS

I am used to jQuery , which has a delegate() method.我习惯了jQuery ,它有一个delegate()方法。 So let's say I have HTML like this:所以假设我有这样的 HTML :

<div id="covers">
   <a href="#"><img ... /></a>
   <a href="#"><img ... /></a>
   <a href="#"><img ... /></a>
   <a href="#"><img ... /></a>
   <a href="#"><img ... /></a>
   <a href="#"><img ... /></a>
   ...lots anchors here...
</div>

In jQuery, I can bind events to the parent div and not attach individual handlers to new, dynamically added anchors using delegate() like so:在 jQuery 中,我可以将事件绑定到父 div,而不是使用delegate()将单个处理程序附加到新的、动态添加的锚点,如下所示:

$('#covers').delegate('a', 'click', function(){
   /// stuff I want to do
   var clickedAnchor = $(this);
});

What would the equivalent code be in Prototype ? Prototype中的等效代码是什么?

Prototype's philosophy is that you do it more like in pure javaScript only with added cross-browser abstractions (plus releasing once a year, plus polluting native prototypes). Prototype 的理念是你做的更像是在纯 javaScript 中,只是添加了跨浏览器抽象(加上每年发布一次,加上污染原生原型)。

So there's no delegate in Prototype. 所以原型中没有 delegate You can use Event#findElement though.不过,您可以使用Event#findElement

$('covers').observe('click', function (event) {
  var link = event.findElement('a');

  if (link) {
      // ...
  }
});

Edit: There is delegate in Prototype: http://api.prototypejs.org/dom/Event/on/ !编辑:原型中delegatehttp://api.prototypejs.org/dom/Event/on/

$('covers').on('click', 'a', function (event, element) {
   // ...
});

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

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