我在我的页面上有一个多个菜单,它们全部使用相同的鼠标悬停和点击事件,因此我决定将它变成一个函数。然而,变量似乎总是被分配给悬停(函数,函数)函数的最后一个参数。jQuery和关闭
$(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将始终记录最后活跃而不是属于参数[I]。活跃的一个。 (在这个例子中,它始终记录下有效的的参数[1] .active) 我在做什么错了?
此外,真正的功能更复杂,但问题也出现在这个变体中。
非常感谢。 – 2009-01-29 18:26:00