어떻게 이것을 루프에 집어 넣고 변수 이름을 동적으로 지정할 수 있습니까?
이이 나던자바 스크립트에서 동적으로 변수 이름을 지정하십시오.
var aMainNav = ['b1', 'b2', 'b3'];
var i = 0;
while(i < aMainNav.length){
var j = i++;
eval(aMainNav[i] = $('.mainNav li:nth-child('+j+')'));
i++;
}
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)');
작동 버튼을
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;
});
'eval' 사용 피하기 –
'b1', 'b2' 등의 목표는 무엇입니까? 두 가지 예에서 모두 덮어 쓴 것입니다. –
전체 코드를 다시 디자인 할 수 있다고 생각합니다.'.click' 줄은 거의 동일합니다. – pimvdb