2015-01-22 3 views
0

이 이벤트는 사용자가 albumPreview 요소를 클릭하면 계속됩니다. 이것은 각 albumPreview을 한 번 클릭 할 때 잘 작동하지만 다시 클릭하면 다시 추가되지 않습니다.기존 콘텐츠를 제거하고 새로 추가

replaceWIth()albumPreview에있는 임의의 수의 요소로 인해 방법이 작동하지 않습니다. 이 작품을 어떻게 만들 수 있습니까? 내 실수가 어디 있는지 이해

albumContent.empty(); // remove previous content 

for (i = 0; i < photoArray.response.length; i++) { 
    albumContent //CONTAINER FOT IMAGES 
     .append($("<div class='img-container-wrap'></div>") 
      .append($("<div class='img-container'></div>") //APPEND TO IMG-CONTAINER-WRAP 
       .css({ 
        "background-image": 'url("' + photoArray.response[i].src + '")' 
       }))) 
} 
+0

.html (''). 추가. .html ('')은 컨테이너의 html을 지우고 append는 새로운 html을 추가 할 것입니다. – Van

+3

@Van 왜 html ('')'대신'empty()'를 사용합니까? –

+0

네, 똑같은 일을 수행 할 것이며 아마도 더 나은 선택 일 것입니다. 그러나 둘 다 작동합니다. – Van

답변

0

:

if ($('.img-container-wrap').length == 0) { //IF NOT EXIST, CREATE 
    console.log(photoArray.response.length); 
    for (i; i < photoArray.response.length; ++i) { 
     albumContent //CONTAINER FOT IMAGES 
     .append($("<div class='img-container-wrap'></div>") 
       .append($("<div class='img-container'></div>") //APPEND TO IMG-CONTAINER-WRAP 
         .css({ 
          "background-image": 'url("' + photoArray.response[i].src + '")' 
         }))) 
    } 
} else if ($('.img-container-wrap').length > 0) { //IF EXIST, REMOVE _AND_ APPEND NEW CONTENT 
    $(".album-content-section").find('.img-container-wrap').remove(); //REMOVE ELEMENTS 
    for (i; i < photoArray.response.length; ++i) { 
     albumContent //CONTAINER FOR IMAGES 
     .append($("<div class='img-container-wrap'></div>") 
       .append($("<div class='img-container'></div>") 
         .css({ 
          "background-image": 'url("' + photoArray.response[i].src + '")' 
         }))) 
    } 
0

빈 컨테이너는 먼저 다음 루프를 할 empty()for에 있었다 loop.But photoArray.response.length하지 무한대 때입니다 루프 마감, empty() 방법 정지 모두들 감사합니다. 얘들 아!

+0

죄송합니다. 동일한 결과가 있습니다. – Max

관련 문제