拥有这段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 select触发事件,并在回调中引用每个选择。我怎么能拥有它呢?这就是小提琴:http://jsfiddle.net/EWUCG/5/
发布于 2011-12-20 02:43:31
来自IRC频道的聊天:
我剩下的唯一解决方案就是"eaching":
$('modal').addEvent('change:relay(.country)', function(event, target){
console.log(this, event, target); // Then "this" refers to each .country select.
});
$$('.country').each(function(el){
$('modal').fireEvent('change', [null, el]);
});发布于 2011-12-20 02:22:38
$$('.country').addEvent('change', function(){
console.log(this);
// "this" refers to select
console.log(this.getElement(':selected'));
// this.getElement(':selected') refers to selected option
}).fireEvent('change');发布于 2012-01-20 06:04:33
这可能就是你想要的.我在一年多前在IRC中发现了这一点,我不能告诉你是谁提供的。
和一个更好的代码重用的元素方法- http://jsfiddle.net/prbNK/12/
https://stackoverflow.com/questions/8565225
复制相似问题