簡體   English   中英

在javascript中打印對象數組,並在html標簽中打印

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

我正在使用storelocater.js在Google地圖中的多個位置,並根據帶有圖像的位置顯示信息。 我只能顯示一個圖像,但我想在信息面板中顯示多個圖像。 連結這個

圖片我想展示的方式

這是我的代碼

    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:意外的令牌為...

我該如何解決呢? 我如何獲取“圖像”內部的位置

提前致謝

我認為有一個錯字。 更改此:

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

實際上,您收到了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 +=將字符串的串聯與html變量和for循環邏輯分開,而不是僅在多行上使用帶有+號的串聯。
  • 在訪問對象時,請確保將屬性名稱包裝在兩個''之間,例如,在this.props_[images]中應為this.props_['images'] ,在this.props_[images[i]]中應將屬性名是this.props_['images'][i]
  • 以及html變量聲明和連接的前兩行, var html = ''; html += '<div class="store-data">'; var html = ''; 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