0

我試圖刪除和替換「文章」標籤由新的使用作爲源json數組,但我總是隻獲取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> 

這裏是我的javascript函數,使用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「李「標籤似乎有隱含的迭代,標籤«文章»,«div»,«span»不。

希望我很清楚,任何輸入將不勝感激。

謝謝!

回答

0

我終於解決了我的問題與此解決方案,基本上我使用jQuery .remove()方法而不是.empty()之一,它的作品完美。

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