简体   繁体   English

在jquery循环中生成多个div

[英]Generate multiple div in jquery loop

I need to ask for help. 我需要寻求帮助。 Having this code: 具有以下代码:

    $(".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('');
            });        
        }); 
    });

I'd like to create a div for each looping. 我想为每个循环创建一个div。 How could I do? 我该怎么办? Thanks for your help. 谢谢你的帮助。

With jQuery it's very easy, look at this JSFiddle http://jsfiddle.net/Ldh9beLn/ 使用jQuery,这非常容易,请查看此JSFiddle http://jsfiddle.net/Ldh9beLn/

I use the function jQuery appendTo() to insert each div inside another div with id = "inserts" if you don't understand a section of this code or how to adapt to yours leave me a comment. 如果您不了解这段代码的一部分,或者如何适应您的代码,请使用jQuery appendTo()函数将每个div插入id =“ inserts”的另一个div中。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM