for 루프를 사용하여 동적 div를 생성 중이며 출력에 놀랐습니다.for 루프 내 실행 순서 - jQuery
이for(continent in CityList)
{
$('<div id="'+continent+'Display"><div class="row top-buffer">').appendTo('#Display');
currentcontinent = CityList[continent];
for(city in currentcontinent) {
$('<div class="col-md-4"><div class="thumbnail"><div class="caption"><h3>'+CityList[continent][city]['EngName']+'</h3><a data-name="'+city+'" href="#'+city+'"> <img src="'+CityList[continent][city]['src']+'" style="min-height:250px;height:250px;"/></a></div></div></div>').appendTo('#Display');
}
$('</div></div>').appendTo('#Display');
}
alert($("div#Display").clone().html());
를 사용하여 출력 HTML은 다음과 같습니다 : 코드는 다음과 같습니다
<div id="AmericaDisplay"><div class="row top-buffer"></div></div><div class="col-md-4"><div class="thumbnail"><div class="caption"><h3>Boston</h3><a data-name="Boston" href="#Boston"> <img src="undefined" style="min-height:250px;height:250px;"></a></div></div></div>...
당신은 </div></div>
첫 번째 APPEND 후,하지만 그 예상이있다 볼 수 있듯이 내가 for 루프에서 마지막으로 호출 한 이후로 html의 끝 부분에 있어야한다. 루프가 이런 식으로 행동해야하는 이유가 있습니까?
'data' 변수의'var' 키워드를 생략함으로써 실수로 글로벌 변수의 사용을 권장합니다. 이런 전역적인 'data' 변수를 생성하고 싶지는 않습니다. 당신이 적어도 그것을 기대할 때 그것은 부랑아에 당신을 물 것입니다. – GregL
고맙습니다. 해결되었습니다. –