【问题标题】:print array of object in javascript and print in html tags在javascript中打印对象数组并在html标签中打印
【发布时间】:2017-10-10 08:37:51
【问题描述】:

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

这是我的代码

    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...

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

提前致谢

【问题讨论】:

  • 您能否添加您面临的问题/错误?或者可能是如何呈现的?
  • 我已经编辑了我的代码。请查看。
  • @NiranjanShakya 你检查了下面的my answer 吗?!
  • @chsdk 是的,谢谢它对我有用。

标签: javascript arrays string google-maps for-loop


【解决方案1】:

实际上你得到了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 += '&lt;div class="store-data"&gt;'; 可以缩短为var html = '&lt;div class="store-data"&gt;';。

【讨论】:

    【解决方案2】:

    我认为有一个错字。改变这个:

    console.log(this.props_[images[i]])
    

    到

    console.log(this.props_['images'][i])
    

    你应该使用

    i < this.props_['images'].length
    

    所以试试这个:

    for (var i = 0; i < this.props_['images'].length; i++) {
        console.log(this.props_['images'][i]);
    }
    

    【讨论】:

    • 有趣的部分是完整的错误信息。请将此添加到您的问题中。
    猜你喜欢
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多