2014-10-30 121 views
-1

我需要尋求幫助。 擁有此代碼:在jquery循環中生成多個div

$(".apprezzamenti").click(function() 
    {  
     var id = $(this).attr('id'); 

     $.get("http://localhost/laravel/public/index.php/apprezzamenti_modal/"+id, 
     function(data) 
     { 
      data_parsed = JSON.parse(data); 

      // BELOW is the loop that I would like to generate multiple div using .append() 

      for(var i=0; i<data_parsed.length; i++) 
      { 
       var html = '<img src="images/provvisorie/immagine-profilo.png" width="30px" and height="30x" />'; 
        html += ' '; 
        html += "<a href='http://localhost/laravel/public/index.php/utente/" + data_parsed[i].id_user + "' style='font-weight: bold;'>" + data_parsed[i].username + "</a>"; 

       $(".modal-body-apprezzamenti>p").append(html).append('.modal-body-apprezzamenti>p').append($('<br/>')); 
      } 

      $(".chiudi-popup").click(function() 
      { 
       $(".modal-body-apprezzamenti>p").html(''); 
      }); 

      $('#main').click(function() 
      { 
       $(".modal-body-apprezzamenti>p").html(''); 
      });   
     }); 
    }); 

我想爲每個循環創建一個div。 我該怎麼辦?謝謝你的幫助。

+0

目前,循環在哪裏? – briosheje 2014-10-30 15:44:21

+0

你想在哪裏創建一個div? – 2014-10-30 15:45:21

+0

你應該清楚你的問題。並且請添加最好的小提琴。將請求的數據作爲小提琴中的靜態數據 – murli2308 2014-10-30 15:47:32

回答

2

使用jQuery這很容易,看看這個的jsfiddle http://jsfiddle.net/Ldh9beLn/

我使用功能的jQuery appendTo()來插入每個DIV使用id =「插入」另一個DIV中,如果你不理解的部分此代碼或如何適應您的問題留下我的評論。

var things = ["apple", "house", "cloud"]; 

for (var i = 0; i < things.length; i++) { 
    $("<div>"+things[i]+"</div>").appendTo("#inserts"); 
} 
+0

它的工作原理!謝謝! – Francesco 2014-10-30 15:53:42

+0

你能解釋一下代碼嗎? @ starless13 – 2015-06-15 05:49:02