我怎样才能将它压缩成一个循环并动态命名变量?
这行得通
var aMainNav = ['b1', 'b2', 'b3'];
aMainNav[0] = $('.mainNav li:nth-child(1)');
aMainNav[1] = $('.mainNav li:nth-child(2)');
aMainNav[2] = $('.mainNav li:nth-child(3)');
这不是
var aMainNav = ['b1', 'b2', 'b3'];
var i = 0;
while(i < aMainNav.length){
var j = i++;
eval(aMainNav[i] = $('.mainNav li:nth-child('+j+')'));
i++;
}
按钮点击
aMainNav[0].click(function(){
myLocation=='abc'?top.location.href='https://mydomain.com?var=1':window.location.href='./';
return false;
});
aMainNav[1].click(function(){
myLocation=='abc'?top.location.href='https://mydomain.com?var=2':window.location.href='./';
return false;
});
aMainNav[2].click(function(){
myLocation=='abc'?top.location.href='https://mydomain.com?var=3':window.location.href='./';
return false;
});