0

document.write(makeCalendar(2013,0))ここに、最初のパラメーターで選択した年と月に基づいてカレンダーを作成する Java スクリプトがあり、2 番目のパラメーターは月です。その日のカレンダーイベントのみを表示するところまで作業しましたが、残りの日付を表示することはできません。これHolidayName[]が私の配列であり、ループしてイベントを表示しますが、後で新しいイベントが追加されます. 最初の日付のみを表示できますが、ずっとループしている必要があるため、理由がわかりません。以下は私のループで、その後にJavaScriptが続きます。

var HolidayName = new Array (0, 1, "New Years Day",6, 1, "Canada Day",11, 25, "Christmas Day",11, 26, "Boxing Day")
function getHoliday(month, day)
{
    for(var index = 0; HolidayName.length > index; index++)
    {
        if(HolidayName[index] == month && HolidayName[index+1] == day)
        {
            var name = HolidayName[index+2]
        }
        else
        {
            return ""
        }
        return name
    }
}   

以下はコードで、イベントは関数のショーの日付セクションに表示されていますgetHoliday(mth, dayCtr)

function leapYear(yr) { 
if (yr < 1000) yr+=1900
return((yr%4 == 0) && ((yr%100 == 0) || (yr%400 ==0)))
}

function startCol(width, height, color){
return('<TD WIDTH=' + width + ' HEIGHT=' + height + '>' + '<FONT COLOR="' + color + '">');
}

function makeCalendar(yr, mth){

var months    = new Array("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec")
var days      = new Array(31, leapYear(yr)?29:28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
var weekDays  = new Array("Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat")
var HolidayName = new Array (0, 1, "New Years Day",6, 1, "Canada Day",11, 25, "Christmas Day",11, 26, "Boxing Day")

function getHoliday(month, day)
{
    for(var index = 0; HolidayName.length > index; index++)
    {
        if(HolidayName[index] == month && HolidayName[index+1] == day)
        {
            var name = HolidayName[index+2]
        }
        else
        {
            return ""
        }
        return name
    }
}   

var mthSz         = days[mth]
var mthName       = months[mth]
var firstDyofMnth = new Date(yr, mth, 1)
var firstDay      = firstDyofMnth.getDay() + 1
var numRows       = Math.ceil((mthSz + firstDay-1)/7)
var mthNameHeight = 50

var borderWidth   = 2
var cellSpacing   = 4 
var cellHeight    = 80 

var hdrColor      = "midnightblue" 
var hdrSz         = "+3" 
var colWidth      = 100 

var dayCellHeight = 25 
var dayColor      = "black" 
var dayCtr    = 1


// Build the HTML Table 
var txt = '<CENTER>'
txt += '<TABLE BORDER=' + borderWidth + ' CELLSPACING=' + cellSpacing + '>' 

//Show Month Name and Year
txt += '<TH COLSPAN=7 HEIGHT=' + mthNameHeight + '>' 
txt += '<FONT COLOR="' + hdrColor + '" SIZE=' + hdrSz + '>' 
txt += mthName + ' ' + year + '</FONT>' + '</TH>'

// Show Days of the Week 
txt += '<TR ALIGN="center" VALIGN="center">'
for (var dy = 0; dy < 7; ++dy) {
    txt += startCol(colWidth, dayCellHeight, dayColor) + weekDays[dy] + '</FONT></TD>' 
}
txt += '</TR>'

// Show Dates in Calendar
for (var row=1; row <= numRows; ++row) {
    txt += '<TR ALIGN="right" VALIGN="top">'
    for (var col = 1; col <= 7; ++col) {
        if (((col < firstDay) && (row==1)) || (dayCtr>mthSz))
            {txt += '<TD BGCOLOR="Gainsboro"><BR></TD>'}
        else
            {
            txt += '<TD HEIGHT=' + cellHeight + '><FONT COLOR="' + dayColor + '"> <B>'
            txt += dayCtr 
            txt += '</B></FONT><BR>' + getHoliday(mth,dayCtr) + '</TD>'
            dayCtr++;
            }
    }
    txt += '</TR>'
}

// close all basic table tags and output txt string
txt += '</TABLE></CENTER>'
document.write(txt) 

}
4

1 に答える 1

0

Because you have else { return "" } and then outside the if...else statement, return name. So whether the condition is true or false, your function will terminate during the first run of the loop, returning the name if the condition is true, or an empty string otherwise.

Also, I think you need to increment index by 3, not by 1, on each loop; and you should use semi-colons to end each line (unless it is the start or end of a {} block).

var HolidayName = new Array(0, 1, "New Years Day", 6, 1, "Canada Day", 11, 25, "Christmas Day", 11, 26, "Boxing Day");

function getHoliday(month, day) {
  for (var index = 0; index + 2 < HolidayName.length; index+=3) {
    if (HolidayName[index] == month && HolidayName[index + 1] == day) {
      return HolidayName[index + 2];
    }
  }
  return '';
}
于 2013-01-11T00:05:43.053 に答える