0

新しいdivに画像と名前が表示されています。ただし、次に人が名前と写真を入力して保存を押すと、userDiv に自分の名前と以前のユーザー名が表示されるという問題が発生しました。たとえば、User 1 と User 2 の 2 人のユーザーがいます。すべてのユーザーを選択して結果をループすると、名前が異なってログに記録されます。ユーザー 1 は「ユーザー 1」として表示されますが、ユーザー 2 は「ユーザー 1 ユーザー 2」として表示されます。ざっと見てみると、innerHTMLが親divからすべてのコンテンツを取得しているためだと思いますが、よくわかりません。

    var htmlStr="";
  var theID;
  var theName;
  var thePhoto;
  var len = results.rows.length;
  console.log(len);
    for(var i = 0; i < len; i++){
        theID = results.rows.item(i).id;
        console.log(theID);
        theName = results.rows.item(i).username;
            htmlStr += theName;
        console.log(theName);
        thePhoto = results.rows.item(i).imagepath;
        console.log(thePhoto);

        var imageHold= new Image();
        imageHold.src = thePhoto;
        console.log("this is the src:"+imageHold.src);
        var userDiv = document.createElement("div");//Create the div
        userDiv.innerHTML=htmlStr;
        userDiv.appendChild(imageHold);
        document.getElementById('showUsers').appendChild(userDiv);//append it to the document
        userDiv.style.display = 'block';
4

1 に答える 1

2

各反復のループ内でそれを削除var htmlStr="";して宣言します。だから変える

htmlStr += theName;

var htmlStr = theName;

これで問題が解決するはずです

于 2013-01-17T14:28:22.930 に答える