これをループに凝縮して、変数に動的に名前を付けるにはどうすればよいですか?
これは動作します
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;
});