2013-02-25 83 views
0

我想通過這個數組迭代到地圖上的引腳,在每個瀏覽器中它似乎工作正常,但在IE8中我得到錯誤「SCRIPT5007:無法獲取屬性的值'top':object is null or undefined「如果我將top改成別的東西或刪除它,IE只是說左邊或者名字相同的東西等。如果有人有任何建議,他們將不勝感激。Json對象屬性undefined只在IE8中

jQuery(document).ready(function(){ 
    var pins = [ 
      { "name":"Mexico", "top":"154" , "left":"176", "imageFile":"Mexico.jpg", "soundMP3File":"Mexico_GMartinez_1.mp3", "soundOGGFile":"Mexico_GMartinez_1.ogg"}, 
      { "name":"Chile", "top":"261" , "left":"246", "imageFile":"Chile.jpg", "soundMP3File":"Chile_MVelasquez_1.mp3", "soundOGGFile":"Chile_MVelasquez_1.ogg"}, 
      { "name":"Trinidad and Tobago", "top":"182" , "left":"242", "imageFile":"TrinidadTobago.jpg", "soundMP3File":"Trinidad-Tobago_SHarris_1.mp3", "soundOGGFile":"Trinidad-Tobago_SHarris_1.ogg"}, 
      { "name":"Columbia", "top":"209" , "left":"227", "imageFile":"Columbia.jpg", "soundMP3File":"Colombia_JGordon_2.mp3", "soundOGGFile":"Colombia_JGordon_2.ogg"}, 
      { "name":"Canada", "top":"86" , "left":"208", "imageFile":"Canada.jpg", "soundMP3File":"Canada_KBretzer_1.mp3", "soundOGGFile":"Canada_KBretzer_1.ogg"},    
      ]; 

for(var i = 0; i < pins.length; i++) 
    { 
     jQuery('#map #pins-container').append('<a href="#" class="pin" style="top: ' + pins[i].top + 'px; left:' + pins[i].left + 'px;" pinindex="' + i + '" name="' + pins[i].name + '"></a>'); 
    } 


}); 
+5

你有數組中的最後一個對象後尾隨逗號。刪除。 – Guffa 2013-02-25 14:51:28

回答

3

@Guffa是對的。 「加拿大」行後面的逗號無效。

IE是唯一一個實際上會抱怨它的瀏覽器,但它在任何地方都是技術上無效的。

爲了避免這樣的問題,您應該使用像JSHint這樣的工具,它將驗證您的Javascript代碼以編寫常見錯誤,包括尾隨逗號錯誤。

+0

注意:JShint已集成到JSfiddle.net中。我建議你走那條路。 – Hugolpz 2013-02-25 15:56:21

0

IE如果你給逗號最後JSON對象後,這是給錯誤IE6,IE7,IE8。

所以,一定要記住這個當過你正在編寫JavaScript,JSON