如何在ajax中附加html代码块

时间:2015-10-09 06:08:57

标签: javascript jquery ajax

我的jsp页面中有一个代码块。我想在带有append标签的ajax中使用此代码。 这是我的代码。

for ( var i = 0, len = data.length; i < len; ++i) {
  var listValue = data[i];
  $('.flowers').append(
    '<div class="profile-sidebar" style="width: 250px;">\
      <div class="portlet light profile-sidebar-portlet" \
        data-category="listValue.officeNameNoSpace"> \
        <div class="profile-userpic"> \
          <img src="listValue.imgPath" class="img-responsive" \
          alt=""> \
        </div> \
        <div class="profile-usermenu"></div> \
      </div> \
    </div>'
  );
}

但它仍有错误。这是错误:

String literal is not properly closed by a matching quote

我试过这种方式。:

for ( var i = 0, len = data.length; i < len; ++i) {
  var listValue = data[i];
  $('.flowers').append(
    '<div class="profile-sidebar" style="width: 250px;">
      <div class="portlet light profile-sidebar-portlet"
        data-category="listValue.officeNameNoSpace">
        <div class="profile-userpic">
          <img src="listValue.imgPath" class="img-responsive"
          alt="">
        </div>
        <div class="profile-usermenu"></div>
      </div>
    </div>'' +
');

我该怎么办?感谢。

1 个答案:

答案 0 :(得分:0)

它有点过时,但为什么不尝试下面的字符串连接:

for ( var i = 0, len = data.length; i < len; ++i) {
                var listValue = data[i];
                $('.flowers').append('<div class="profile-sidebar" style="width: 250px;">'+

                        '<div class="portlet light profile-sidebar-portlet"'+
                            'data-category="'+listValue.officeNameNoSpace+'">'+
                            '<div class="profile-userpic">'+
                                '<img src="'+listValue.imgPath+'" class="img-responsive"  alt="">'+
                            '</div>'+

                            '<div class="profile-usermenu"></div>'+
                        '</div>'+

                    '</div>');
        }

告诉我它是否有效。