0

json 배열을 소스로 사용하여 'article'태그를 삭제하고 "article"태그를 대체하려고합니다.하지만 항상 JSON 배열의 마지막 요소 만 얻고 있습니다 ... 여기 방법 내 HTML 구조를 보면 : 여기Wordpress에서 AJAX로 게시물을 제거하고 바꿉니다

<main id="main" class="site-main" role="main"> 

    <article > 
    <header class="entry-header">    
    TEST 1 
     <a href="http://example.com/mdu_test/mdu_news/4996" rel="bookmark"> test</a> 
     <div>TAGS:<a href="http://example.com/tag/dog">dog</a></div> 
    </header> 
    </article> 

    <article > 
    <header class="entry-header">   
    TEST 2 
     <a href="http://example.com/mdu_test/mdu_news/4587" rel="bookmark"> test</a> 
     <div>TAGS:<a href="http://example.com/tag/cat">cat</a></div> 
    </header> 
    </article> 
</main> 

내 자바 스크립트 함수 인을 제거하고 새 것으로 이전 게시물을 대체 할 jQuery를 사용하여.

function get_posts($params){ 
    var $container = $('#main'); 
    var $content = $container.find('article'); 
    var $status = $('.ajax_tag'); 
    var counter = 0; 

... Jquery.ajax 문 여기 ...

$.each(data,function(index, post){ 
    //$content.children().remove(); 
    $content.empty(); 
    console.log(index); 
    //$content.replaceWith('<article>' + post.title + '</article>'); 
    //console.log(data.length); 
    if (counter != data.length) { 

    console.log('before : ' + counter); 
    $content.append('<article>' + post.title + '</br>' + '</article>'); 
    counter += 1; 
    console.log('after : ' + counter); 

    } else { 
     return false; 
    } 
}); 

그래서 여기에서 내가 그 작업을 수행 할 수있는 방법을 나에게 분명하지 않다.

내 배열의 전체 목록을 관리하는 유일한 방법은«. append()»를 사용했지만«.empty()»를 사용하지 않고 각«article»태그에 배열 내용을 추가하는 것뿐입니다.

추 신 : .each(), $ .each(), .map(), $ .map() for 및 for ... 모든 예제와 설명에서 CSS " li "태그는«article»,«div»,«span»태그는 암시 적 반복이있는 것으로 보입니다.

희망은 충분히 명확합니다. 모든 의견을 많이 보내 주시면 감사하겠습니다.

감사합니다.

답변

0

나는 결국이 해결책으로 내 문제를 해결했다. 기본적으로 나는 .empty() 대신에 .remove() jQuery 메서드를 사용하고 있으며 완벽하게 작동한다.

$.each(data,function(index, post){ 
     $content.remove(); 
     $content.append('<article>' + post.title + '</br>' + '</article>'); 
});