简体   繁体   English

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

[英]print array of object in javascript and print in html tags

i am using storelocater.js for multiple location in google map and show the information according to the location with image. 我正在使用storelocater.js在Google地图中的多个位置,并根据带有图像的位置显示信息。 i can show only one image but i want to show multiple images inside the information panel. 我只能显示一个图像,但我想在信息面板中显示多个图像。 link this 连结这个

图片我想展示的方式

Here is my code 这是我的代码

    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"]})
  ]);

and it shows: Uncaught SyntaxError: Unexpected token for... 它显示:Uncaught SyntaxError:意外的令牌为...

how can i solve this?? 我该如何解决呢? how can i fetch location inside of "images" 我如何获取“图像”内部的位置

THANKS in advance 提前致谢

I think there is a typo. 我认为有一个错字。 Change this: 更改此:

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

to

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

And you should use 你应该使用

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

So try this: 所以试试这个:

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

Actually you got Uncaught SyntaxError: Unexpected token for... because you used the for..loop in the string concatenation statement, directly after the + sign. 实际上,您收到了Uncaught SyntaxError: Unexpected token for...因为您在字符串连接语句中直接在+号之后使用了for..loop

Change this code : 更改此代码:

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

To the following: 要以下内容:

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

Note: 注意:

  • You should separate the concatenation of strings to the html variable and the for loop logic, using html += instead of just using concatenation with + sign on multiple lines. 您应该使用html +=将字符串的串联与html变量和for循环逻辑分开,而不是仅在多行上使用带有+号的串联。
  • Make sure to wrap the properties names between two '' while accessing your objects, like in this.props_[images] where it should be this.props_['images'] and in this.props_[images[i]] where it should be this.props_['images'][i] . 在访问对象时,请确保将属性名称包装在两个''之间,例如,在this.props_[images]中应为this.props_['images'] ,在this.props_[images[i]]中应将属性名是this.props_['images'][i]
  • And the first 2 lines of your html variable decalaration and the concatenation, var html = ''; html += '<div class="store-data">'; 以及html变量声明和连接的前两行, var html = ''; html += '<div class="store-data">'; var html = ''; html += '<div class="store-data">'; can be shortened to just var html = '<div class="store-data">'; 可以简化为var html = '<div class="store-data">'; .

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

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