简体   繁体   English

jQuery追加多个div

[英]JQuery append with multiple divs

I am currently trying to post certain blocks of code every time an Ajax call is loaded. 我目前正在尝试在每次加载Ajax调用时张贴某些代码块。 I am using Append to do it and it looks like this: 我正在使用Append来做,它看起来像这样:

$("#wrap").append("
   <div class='newData'>
     <div class='infoBox'>
       " + firstName + lastName + "
     </div>
   </div>"
);

My Ajax looks like this: 我的Ajax看起来像这样:

$.ajax({
        type: "GET",
        url: "feed.php",
        dataType: 'json',
        success: function(data) {
          for(i = 0; i < data.length; i++){
            firstName = data[i].First_Name;
            lastName = data[i].Last_Name;

           // wrap.innerHTML += '<div class="newData">' + firstName + lastName +'</div>';
            $("#wrap").append("
              <div class='newData'>
                <div class='infoBox'>
              " + firstName + lastName + "
                </div>
              </div>");
          }
        }
    });

When I try to do this nothing comes up. 当我尝试这样做时,什么也没有发生。 If I take out the infoBox div I get it to work. 如果我将infoBox div取出,则可以正常使用。 Is this the proper way to do this. 这是执行此操作的正确方法吗? I will be adding multiple different divs inside of this with variables from my Ajax call. 我将使用来自Ajax调用的变量在其中添加多个不同的div。 Are there better ways to do this or is this the right approach but done wrong? 有更好的方法可以做到这一点吗?或者这是正确的方法却做错了吗?

You aren't concatenating it properly. 您没有正确串联它。

It should look like this: 它看起来应该像这样:

 $("#wrap").append("<div class='newData'>" +
                      "<div class='infoBox'>" +
                        firstName + lastName + 
                      "</div>" +
                   "</div>");

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

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