1

Stackoverflow でこの問題を検索する方法がわかりません。さまざまな解決策があり、私の問題とは関係ありません。だから私は再び皆さんの助けが必要になります.

変数 obj の値:

item.title = Title1 and Title2

item.description = Description1 and Description2

HTML ファイル:

function test_pop() {
    var fb_id = "xxxxxxxxxxxx";
    var v_id = "xxxxxxxx";

    $.post('http://mysite.com/myfile.php', {fb_id: fb_id, v_id: v_id}, function(data) {
        var obj = $.parseJSON(data);
        $.each(obj,function(index, item){
           achievement = "You Obtained " + item.title + " " + item.description;
           achievement_pop(achievement);
        });
    });
}

achievement_pop 関数:

function achievement_pop(data) {
    $("#achievement_popup").html(data).show(); //DIV that should show up
}

したがって、問題は、div が表示されると、次のようにしか出力されないことです。

    <div> <!-- Imagine that this content is inside a DIV -->
**You Obtained Title2 Description2**

    </div>

しかし、私の欲望の出力は次のとおりです。

    <div> <!-- Imagine that this content is inside a DIV -->

**You Obtained Title1 Description1**

**You Obtained Title2 Description2**

    </div>

皆さんが私を助けてくれることを願っています、ありがとう。

4

2 に答える 2

3

html関数は div のコンテンツ全体を置き換えています。追加するだけではありません。

解決策は交換することです

$("#achievement_popup").html(data).show(); 

$("#achievement_popup").html($("#achievement_popup").html()+data).show(); 

しかし、それは少し重いです。個人的には、最初に html を作成し、html関数を 1 回だけ使用します。

于 2013-01-02T07:07:18.807 に答える
2

append()要素の最後に追加し、コンテンツを完全に置き換えないために使用しますhtml()

または、既存のコンテンツをすべての新しいコンテンツで一度に置き換えたい場合:

/* start with empty string*/
var achievement='';
$.each(obj,function(index, item){
    /* add to string on each pass of loop - can use any html you want here*/
     achievement += "You Obtained " + item.title + " " + item.description +'<br>';
});
/* update element once all data parsed*/
 achievement_pop(achievement);

文字列に解析して 1 回の更新を行うことが、処理の観点から最も効率的です

于 2013-01-02T07:09:12.147 に答える