「forEach」を使用して構築した関数を修正しようとしていますが、これは廃止されるはずの Web ブラウザー (IE) と互換性がありません。これをJavascriptで行いました。for ループに変換しようとしましたが、失敗しました。foreach を取り除き、 for ループを使用して 2 つの関数を変換するのを手伝ってくれる人がいれば、助けていただければ幸いです。
参照する 2 つの配列を次に示します。
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);
}