Jquery 每个 html

Jquery each html

我有代码:

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

当我尝试 alert(row.html) 时,我得到了所有结果,但是当我使用上面的代码时。它只将一个结果添加到 div,为什么?

编辑。

已尝试 追加。我的原创div: http://image.prntscr.com/image/3ac6dc5a137e46ababff6acd6bfc2a1a.png

附加后: http://image.prntscr.com/image/6b9a2fc093c0440a8d39afd8db0dc274.png 我想覆盖,而不是添加相同的代码

你可以试试这个:

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

希望对您有所帮助。

你现在正在做的是覆盖元素($( '.topStatsOuter' ).html( row.html );)的内容。

你应该做的是先清空元素的内容,然后使用循环追加结果。您的代码应如下所示:

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