2017-10-10 62 views
1

我正在使用storelocater.js獲取Google地圖中的多個位置,並根據圖像位置顯示信息。我只能顯示一個圖像,但我想在信息面板中顯示多個圖像。鏈接此在javascript中打印對象數組,並在html標記中打印

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

它表明: 未捕獲的SyntaxError:意外的令牌......

我怎樣才能解決這個?我怎樣才能獲取位置的「圖像」

THANKS內提前

+0

請問您可以添加您遇到的問題/錯誤嗎?或者可能是如何渲染? –

+0

我編輯了我的代碼。請檢查。 –

+0

@NiranjanShakya你在下面檢查了[**我的答案**](https://stackoverflow.com/a/46663141/3669624)?! –

回答

0

其實你有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 l oop邏輯,使用html +=而不是僅在多行上使用與+符號的級聯。
  • 確保在訪問您的對象時在兩個''之間打包屬性名稱,如this.props_[images]應該是this.props_['images']this.props_[images[i]]應該是this.props_['images'][i]
  • 並且您的html變量錯位和連接的前2行可以縮短爲var html = '<div class="store-data">';
+0

謝謝。完成。 –

0

我覺得有一個錯字。更改此:

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

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

而且你應該使用

那麼試試這個:

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

它仍然顯示 未捕獲的SyntaxError:意外的令牌... –

+0

有趣的部分是完整的錯誤信息。請將此添加到您的問題中。 – RWAM

+0

同樣的問題... –