2014-10-30 53 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