ページに複数のメニューがあり、それらはすべて同じマウスオーバー イベントとクリック イベントを使用するため、関数に組み込むことにしました。ただし、変数は常に hover(function, function) 関数の最後の引数に割り当てられるようです。
$(document).ready( function() {
menuMouseOver = function() {
for(i=0, u=arguments.length; i<u; i++){
var parent = arguments[i].parent;
var active = arguments[i].active;
var childSelect = arguments[i].childSelect;
console.log(active); //logs the correct active
$(parent).children(childSelect)
.not('.'+active).each( function(i, e) {console.log(active);})
//The above console.log logs the correct active
.hover( function() {
console.log(active); //this one always logs menu2_active
$(this).addClass(active);
}, function() {
$(this).removeClass(active);
});
}
}
menuMouseOver( { parent: '#menu1',
active: 'menu1_active',
childSelect: ':gt(0)'},
{ parent: '#menu2',
active: 'menu2_active',
childSelect: ':gt(0)'});
});
最後の console.log が常に、arguments[i].active に属するものではなく、最後のアクティブをログに記録するのはなぜですか。(この例では、arguments[1].active のアクティブを常にログに記録します) 何が間違っていますか?
また、実際の関数はより複雑ですが、このバリアントにも問題があります。