2012-05-07 76 views
1

嗨我正在調試我的頁面IE8 compat模式,這個腳本只是不喜歡工作和粉碎。IE 8陣列迭代

基本上它必須遍歷一個3D數組,並將一個本地路徑添加到一個變量。嗯,我能做到這一點,否則,但我只是好奇爲什麼* *它永遠不會奏效......

任何建議,歡迎:)下面的代碼:

for(i=0;i<menu_items_p.length;i++) 
for(j=0;j<menu_items_p[i].length;j++) 
menu_items_p[i][j][1]='http://127.0.0.1/'+menu_items_p[i][j][1]; 

和陣列的外觀是這樣的:

var menu_items_p = 
[ 
    [ //Products 
     ['Health Care', 'products/health.php'], 
     ['Aroma Therapy','products/scents.php'], 
    ], 
      [  // Empty 
      ], 
    [ //Test 
     ['What ever', 'spirulina/about.php'], 
    ] 
] 

但問題是,它有時具有空值,而array.length觸發一些錯誤...

+0

如果數組可以有空值,那麼你必須檢查這種情況。只需寫幾行代碼即可完成。 – Yoshi

+0

這並不容易。完美的IE9工作,現在只是不工作...........(typeof menu_items_p [i] .length!=「undefined」)也觸發錯誤,所以它更好地告訴我們什麼代碼應該我們把它,並首先檢查它與IE8 compat模式..謝謝 – Anonymous

+0

你會得到什麼錯誤? 'typeof menu_item_p [i]'返回什麼? (你應該真的使用大括號並正確地聲明變量) – Niko

回答

0

AS由Yoshi和ThiefMaster建議,我做了以下內容,這是解決它:

for(var i=0;i<menu_items_p.length;i++) 
if (menu_items_p[i] !== undefined) 
for(var j=0;j<menu_items_p[i].length;j++) 
if (menu_items_p[i][j] !== undefined) 
menu_items_p[i][j][1]='http://127.0.0.1/'+menu_items_p[i][j][1]; 
  1. 全球瓦爾斯取而代之。
  2. 檢查未定義。

可惜的是他們沒有正式的方式回答,所以我會回答我的自我:) 謝謝大家!

3

當您使用r原始數組聲明:

var menu_items_p = 
[ 
    [ //Products 
     ['Health Care', 'products/health.php'], 
     ['Aroma Therapy','products/scents.php'], 
    ], 
      [  // Empty 
      ], 
    [ //Test 
     ['What ever', 'spirulina/about.php'], 
    ] 
] 

錯誤發生在IE8中,但不在IE9中。只刪除兩個逗號:

var menu_items_p = 
[ 
    [ //Products 
     ['Health Care', 'products/health.php'], 
     ['Aroma Therapy','products/scents.php'] // here comma removed 
    ], 
      [  // Empty 
      ], 
    [ //Test 
     ['What ever', 'spirulina/about.php'] // here comma removed 
    ] 
] 

和所有必須正常工作。

+0

不幸的是這不是問題,逗號在原始代碼中沒有問題 – Anonymous

0

也許你的代碼可以處理空值是這樣的:

for(var i = 0; i < menu_items_p.length; i++) { 
    // we skip the value if it is empty or an empty array 
    if(!menu_items_p[i] || !menu_items_p[i].length) continue; 
    for(var j = 0; j < menu_items_p[i].length; j++) { 
     // again, we skip the value if it is empty or an empty array 
     if(!menu_items_p[i][j] || !menu_items_p[i][j].length) continue; 
     menu_items_p[i][j][1] = 'http://127.0.0.1/' + menu_items_p[i][j][1]; 
    } 
}