javascript - 在javascript中打印对象数组并在html标签中打印

标签 javascript arrays string google-maps for-loop

我正在使用 storelocater.js 在谷歌地图中显示多个位置,并根据图像位置显示信息。我只能显示一张图像,但我想在信息面板内显示多张图像。链接这个

Image how I want to show

这是我的代码

    var panelDiv = document.getElementById('panel');
storeLocator.Panel.NO_STORES_IN_VIEW_HTML_ = '<li class="no-stores">The nearest outlet:</li>';
  var Store = storeLocator.Store;
  Store.prototype.generateFieldsHTML_ = function(fields) {
    var html = '';
    html += '<div class="store-data">';
    if(this.props_['title']){
      html += '<div class="title"><div class="img-list clearfix">' + 
      for (var i = 0; i <= this.props_[images].length; i++) {
        console.log(this.props_[images[i]]);
        // <img src=' + this.props_['images'] + '>
      }
  + '</div></div>'
    }
    html += '</div>';
    return html;
  }
  var data = new storeLocator.StaticDataFeed;
  data.setStores([
    new storeLocator.Store('store02', new google.maps.LatLng(27.67663,85.31093), null, {images: ["img/thapathalil.jpg","img/thapathalil.jpg","img/thapathalil.jpg"]})
  ]);

它显示: 未捕获的语法错误:意外的标记...

我该如何解决这个问题?我如何获取“图像”内的位置

提前致谢

最佳答案

实际上你有Uncaught SyntaxError: Unexpected token for...因为您使用了 for..loop在字符串连接语句中,紧接在 + 之后标志。

更改此代码:

html += '<div class="title"><div class="img-list clearfix">' + 
for (var i = 0; i <= this.props_[images].length; i++) {
  console.log(this.props_[images[i]]);
  // <img src=' + this.props_['images'] + '>
}
+ '</div></div>'

至以下内容:

html += '<div class="title"><div class="img-list clearfix">';
for (var i = 0; i <= this.props_['images'].length; i++) {
  console.log(this.props_['images'][i]);
  html += '<img src=' + this.props_['images'][i] + '>';
}
html += '</div></div>'

注意:

  • 您应该将字符串的串联与 html 分开。 变量和 for循环逻辑,使用html +=而不是仅仅使用 + 的串联在多行上签名。
  • 确保将属性名称包含在两个 '' 之间访问您的对象时,例如 this.props_[images]它应该在哪里 this.props_['images']并在 this.props_[images[i]]它应该在哪里 this.props_['images'][i] .
  • 以及您的 html 的前两行变量声明和串联,var html = ''; html += '<div class="store-data">';可以缩短为 var html = '<div class="store-data">'; .

关于javascript - 在javascript中打印对象数组并在html标签中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46662162/

相关文章:

python - 在一个字符串中查找多个子字符串,而无需对其进行多次遍历

javascript - Angularjs ctrl 单元测试因依赖项失败

javascript - Javascript 中未定义的数组参数

javascript - Angular(JavaScript)确认密码验证消息?

arrays - 如何从 Swift 中的元组数组创建条纹

arrays - 如何将键值对象插入redis中的lua表

regex - 用 1 个以上的单词拆分字符

c# - string.Format() 给出 "Input string is not in correct format"

Javascript - 清理用户输入的货币值

javascript - 公理。即使 api 返回 404 错误,如何在 try catch finally 中获得错误响应