2017-01-28 23 views
1

我有代碼:jQuery的每個HTML

success: function(res) {     
    if(res.updated.length > 0) { 
     $.each(res.updated, function(k,row) { 
      $('.topStatsOuter').html(row.html); 
     }); 
    } 
} 

當我嘗試警報(row.html),我得到的所有結果,但是當我使用的代碼如上。它只爲div添加一個結果,爲什麼?

編輯。

嘗試追加。我原來的div: http://image.prntscr.com/image/3ac6dc5a137e46ababff6acd6bfc2a1a.png

追加後: http://image.prntscr.com/image/6b9a2fc093c0440a8d39afd8db0dc274.png 我想覆蓋,不添加相同的代碼

+0

嘗試'$( '.topStatsOuter').append(row.html);' - 因爲你目前覆蓋元素的含量(當使用'HTML()') 。您可以在循環前添加'$('.topStatsOuter').empty()'以刪除任何以前的內容 –

+0

@AlonEitan嘗試過,不行的 – EvaldasL

+0

您是否嘗試清空元素'$('.topStatsOuter').emtpy )''.each之前''.each(res.updated,function(k,row){'line? –

回答

1

你現在正在做的是覆蓋元素的內容($('.topStatsOuter').html(row.html);)。

你應該做的是首先清空元素的內容,然後使用循環附加結果。您的代碼應該如下:

success: function(res) {     
    if(res.updated.length > 0) { 
     $('.topStatsOuter').empty(); 
     $.each(res.updated, function(k,row) { 
      $('.topStatsOuter').append(row.html); 
     }); 
    } 
} 
1

你可以試試這個:

success: function(res) {     
    if(res.updated.length > 0) { 
     var html=""; 
     $.each(res.updated, function(k,row) { 
      html+=row.html; 
     }); 
     $('.topStatsOuter').html(html); 
    } 
} 

希望它能幫助。

+1

這也是一個很好的例子 –