2013-05-24 4 views
0

동적 요소 생성과 관련된 문제에 봉착 한 것 같습니다. DOM에 추가 된 요소는 jquery ".children"메소드로 인식되지 않습니다. 데이터가 미리 추가되기 전에 숨기고 배열에 넣은 다음 애니메이션을 적용하고 싶습니다.Jquery 동적 요소가 인식되지 않습니다.

.on() 메서드를 살펴 봤지만 동적 요소를 바인딩하여 인식 할 수 있도록하는 방법이 확실하지 않습니다. 어떤 제안? 감사!

UPDATE FULL 코드 :

$(function() { 
    var children = []; 
    initInstagram(); 

    $("#main").children().each(function() { 
    console.log(this); 
    children.push(this); 
    }); 

animateElements(children); 

}); 

function animateElements(children) { 
if (children.length > 0) { 
    var currentChild = children.shift(); 
    $(currentChild).fadeIn("slow", function() { 
    animateElements(children); 
    }); 
} 
} 

function initInstagram() { 
    $.getJSON('../includes/instagram.php', 
    function(feeds) { 
    var feedHTML = ''; 
    var displayCounter = 1; 
    for (var i = 0; i < feeds.length; i++) { 
     var imgURL = feeds[i].img; 
     var caption = feeds[i].caption;   

     feedHTML = '<article class="box">'; 
     feedHTML += '<img src="'+imgURL+'" alt="'+caption+'" title="'+caption+'" >'; 
     feedHTML += '<div class="overlay"><h4>'+caption+'</h4></div>'; 
     feedHTML += '</article>'; 
     $(feedHTML).prependTo('#main').hide();    
    }   
}); 
}  
+0

이 경우 새 항목이 컨테이너에 추가되면 배열을 별도로 업데이트해야합니다. 배열'children'은 어디에 사용됩니까? –

+0

배열 children은 $ (function() {...});에서 사용됩니다. – cosmoonot

+0

전체 코드를 공유 할 수 있습니까 –

답변

1

, 당신은이 문제를 해결하기 위해 콜백 메커니즘을 사용할 필요가

$(function() { 
    initInstagram(function(feeds, children){ 
     $(children).hide() 
     animateElements(children); 
    }); 
}); 

function animateElements(children) { 
    if (children.length > 0) { 
     var currentChild = children.shift(); 
     $(currentChild).fadeIn("slow", function() { 
      animateElements(children); 
     }); 
    } 
} 

function initInstagram(callback) { 
    $.getJSON('../includes/instagram.php', function(feeds) { 
     var children = []; 
     var feedHTML = ''; 
     var displayCounter = 1; 
     for (var i = 0; i < feeds.length; i++) { 
      var imgURL = feeds[i].img; 
      var caption = feeds[i].caption;   

      feedHTML = '<article class="box">'; 
      feedHTML += '<img src="'+imgURL+'" alt="'+caption+'" title="'+caption+'" >'; 
      feedHTML += '<div class="overlay"><h4>'+caption+'</h4></div>'; 
      feedHTML += '</article>'; 
      var el = $(feedHTML).prependTo('#main');    
      children.push(el.get(0))   
     }  
     callback(feeds, children); 
    }); 
}  

데모 : Plunker

+0

정확하게 작동한다고 생각하지 않습니다. 배열에 동적 요소가 채워지지 않습니다. – cosmoonot

+0

콘솔에 오류가 있습니다. –

+0

오류가 전혀 없습니다. jsfiddle 데모를 만들어야합니까? – cosmoonot

3

나는 DOMNodeInserted 이벤트를 사용하여이 접근 것 :

var children = []; 
$("#main").on('DOMNodeInserted', 'article', function() { 
    console.log(this); 
    children.push(this); 
}); 

// get instagram data 
$.getJSON('../includes/instagram.php', 
    function(feeds) { 
    var feedHTML = ''; 
    var displayCounter = 1; 
    for (var i = 0; i < feeds.length; i++) { 
     var imgURL = feeds[i].img; 
     var caption = feeds[i].caption;   

    feedHTML = '<article class="box">'; 
    feedHTML += '<img src="'+imgURL+'" alt="'+caption+'" title="'+caption+'" >'; 
    feedHTML += '<div class="overlay"><h4>'+caption+'</h4></div>'; 
    feedHTML += '</article>'; 
    $(feedHTML).prependTo('#main');    
}}); 

또는 해당 이벤트가 잘 지원되지 않기 때문에, 트리거에 사용자 정의 이벤트를 만들 삽입 후. 당신은 당신의 코드가 비동기 적으로 실행 얻을 것이다 자식 요소를로드하는 아약스를 사용하고 있기 때문에 아래에 주어진

var children = []; 
$("#main").on('customEvent', 'article', function() { 
    console.log(this); 
    children.push(this); 
}); 

// get instagram data 
$.getJSON('../includes/instagram.php', 
    function(feeds) { 
    var feedHTML = ''; 
    var displayCounter = 1; 
    for (var i = 0; i < feeds.length; i++) { 
     var imgURL = feeds[i].img; 
     var caption = feeds[i].caption;   

    feedHTML = '<article class="box">'; 
    feedHTML += '<img src="'+imgURL+'" alt="'+caption+'" title="'+caption+'" >'; 
    feedHTML += '<div class="overlay"><h4>'+caption+'</h4></div>'; 
    feedHTML += '</article>'; 
    $(feedHTML).prependTo('#main').trigger('customEvent');    
}}); 
+0

DOMNodeInserted 이벤트가 작동합니다. 이것은 옵션입니다. 고맙습니다! – cosmoonot

+0

CustomEvent? 이걸 좀 더 자세히 설명해 주시겠습니까? – cosmoonot

+0

사용자 지정 이벤트는 단순히 이름으로 정의 할 수있는 이벤트입니다. 매개 변수를 전달할 수도 있습니다. trigger() 메소드에 대한 jQuery 문서를 참조하십시오. - http://api.jquery.com/trigger/ – DeweyOx

관련 문제