2012-08-16 54 views
0

我試圖修復使用'forEach'構建的功能,它與應該過時(IE)的Web瀏覽器不兼容。我用Javascript做了這個。我試圖將其轉換爲for循環,但不成功。如果有人可以通過擺脫foreach並使用for循環來幫助我轉換這兩個函數,我將不勝感激任何幫助。切換JavaScript功能從foreach到for循環

這是他們引用的兩個數組。

var yr1 = 2011, yr2 = 2012, yr3 = 2013, yr4 = 2014; 
var years = [yr1, yr2, yr3, yr4]; 
//array with months and associated days 
var calendar = [ 
    ["January", 31],["February", 28],["March", 31],["April", 30],["May", 31],["June", 30],["July", 31],["August", 31],["September", 30], 
    ["October", 31],["November", 30],["December", 31]]; 

這裏是需要從的forEach改爲for循環的功能。

//this creates the month values 
function generateMonths() { 
    var df = document.createDocumentFragment(); 
    calendar.forEach(function(info, i) { 
     df.appendChild(createOption(info[0], i)); 
    }); 
     //clears past months 
    clearChildren(sel_month); 
     //appends new months onto variable df 
    sel_month.appendChild(df); 
} 
//this creates the year values 
function generateYears() { 
    var df = document.createDocumentFragment(); 
    years.forEach(function(i) { 
     df.appendChild(createYearOption(i)); 
    }); 
    //clears past months 
    clearChildren(sel_year); 
    //appends new months onto variable df 
    sel_year.appendChild(df); 
} 

這裏是我的失敗嘗試來證明我嘗試過。

//this creates the month values 
function generateMonths() { 
    var df = document.createDocumentFragment(); 
     for (var w = 0; w < 12; w++) { 
      (function(calendar, w) { 
       df.appendChild(createOption(calendar[0], w)); 
      }); 
     } 
    //calendar.forEach(function(info, i) { 
     //df.appendChild(createOption(info[0], i)); 
    }; 
     //clears past months 
    clearChildren(sel_month); 
     //appends new months onto variable df 
    sel_month.appendChild(df); 
} 
    //this creates the year values 
    function generateYears() { 
    var df = document.createDocumentFragment(); 
     for (var w = 0; w < 12; w++) { 
      (function(years) { 
       df.appendChild(createOption(years[0])); 
      }); 
     }   

     //years.forEach(function(i) { 
     //df.appendChild(createYearOption(i)); 
    }; 
     //clears past months 
    clearChildren(sel_year); 
     //appends new months onto variable df 
    sel_year.appendChild(df); 
} 

回答

1
var years = [yr1, yr2, yr3, yr4]; 

years.forEach(function(i) { 
    df.appendChild(createYearOption(i)); 
}); 

等同於:

for(var i=0; i<years.length; i++) { 
    df.appendChild(createYearOption(years[i])); 
} 

我不知道你的forEach在你的第二個情況是接受。但是我覺得這是你想要做什麼:

var calendar = [["January", 31],["February", 28]]; 

calendar.forEach(function(info, i) { 
    df.appendChild(createOption(info[0], i)); 
}); 

而是使用循環createOption每個陣列的兩個元素。

for(var i=0; i<calendar.length; i++) { 
    df.appendChild(createOption(calendar[i][0], calendar[i][1])); 
} 
+0

那麼另一個呢..我有麻煩了。 – 2012-08-16 12:44:33

+0

沒關係..我知道了..謝謝! – 2012-08-16 12:47:55

2

你總是可以勻forEach如果你想要的。來自MDN

if (!Array.prototype.forEach) { 
    Array.prototype.forEach = function(fn, scope) { 
    for(var i = 0, len = this.length; i < len; ++i) { 
     fn.call(scope || this, this[i], i, this); 
    } 
    } 
} 
+0

+1乍一看,這似乎是提供兼容性的最簡單解決方案。 – 2012-08-16 12:47:32