MooTools事件委托:如何在回调中引用子元素?
有这段 HTML:
<div id="modal">
<select class="country">
<option value=""></option>
<option value="opt">Opt</option>
</select>
</div>
和这段 JS:
$('modal').addEvent('change:relay(.country)', function(){
console.log(this); // "this" refers to #modal.
}).fireEvent('change:relay(.country)');
日志显示 this
关键字引用了 #modal 元素。我想为每个 .country 选择触发事件,并在回调中引用每个国家。我怎样才能拥有它? 这是小提琴: http://jsfiddle.net/EWUCG/5/
Having this piece of HTML:
<div id="modal">
<select class="country">
<option value=""></option>
<option value="opt">Opt</option>
</select>
</div>
And this piece of JS:
$('modal').addEvent('change:relay(.country)', function(){
console.log(this); // "this" refers to #modal.
}).fireEvent('change:relay(.country)');
Log reveals that the this
keyword refers to the #modal element. I want to fire the event for each .country select and have the reference to each one inside the callback. How can I have it?
This is the fiddle: http://jsfiddle.net/EWUCG/5/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 IRC 频道上的聊天:
我剩下的唯一解决方案是“eaching”:
小提琴: http://jsfiddle.net/EWUCG/12/< /a>
From chatting on the IRC channel:
The only solution I have left is "eaching":
Fiddle: http://jsfiddle.net/EWUCG/12/
这可能就是您想要的...我一年多前在 IRC 中发现了这个,但无法告诉您是谁提供的。
http://jsfiddle.net/prbNK/7/
和 Element 方法以实现更好的代码重用 - < a href="http://jsfiddle.net/prbNK/12/" rel="nofollow">http://jsfiddle.net/prbNK/12/
This may be what you are after... I picked this up in the IRC over a year ago and can't tell you who provided it.
http://jsfiddle.net/prbNK/7/
and an Element method for better code reuse - http://jsfiddle.net/prbNK/12/