HTML 組件(HTML COMPONENTS)三
發(fā)布時(shí)間:2008-08-10 閱讀數(shù): 次 來(lái)源:網(wǎng)樂(lè)原科技
===編寫(xiě)日歷一===
當(dāng)calendar.html調(diào)用 MYCAL:CALENDAR,當(dāng)月的日歷將會(huì)顯示在頁(yè)面中,函數(shù)setCal()是主要程序段,它初始化一些變量并調(diào)用drawCal()
函數(shù)。我們也使用了三個(gè)別的函數(shù):getMonthName()、 getDays() 和 leapYear()。讓我們從最后一個(gè)函數(shù)開(kāi)始:
getDays()函數(shù)接收哪月值和哪年值,并且建立一個(gè)有12個(gè)元素的數(shù)組,用來(lái)存放每月的天數(shù),哪一年用來(lái)決定是不是閏年,在閏年中二月
是29天,而不是閏年是28天。該函數(shù)返回指定月份的天數(shù)。以下是getDays():
function getDays(month, year) {
// create array to hold number of days in each month
var ar = new Array(12);
ar[0] = 31; // January
ar[1] = (leapYear(year)) ? 29 : 28; // February
ar[2] = 31; // March
ar[3] = 30; // April
ar[4] = 31; // May
ar[5] = 30; // June
ar[6] = 31; // July
ar[7] = 31; // August
ar[8] = 30; // September
ar[9] = 31; // October
ar[10] = 30; // November
ar[11] = 31; // December
// return number of days in the specified month (parameter)
return ar[month];
}
如果指定的年數(shù)可以被4整除,那么leapYear()函數(shù)將返回“true”,否則返回”false“:
function leapYear(year) {
if (year % 4 == 0) // basic rule
return true; // is leap year
/* else */ // else not needed when statement is "return"
return false; // is not leap year
}
getMonthName()函數(shù)返回指定月份的名字:
function getMonthName(month) {
// create array to hold name of each month
var ar = new Array(12);
ar[0] = "January";
ar[1] = "February";
ar[2] = "March";
ar[3] = "April";
ar[4] = "May";
ar[5] = "June";
ar[6] = "July";
ar[7] = "August";
ar[8] = "September";
ar[9] = "October";
ar[10] = "November";
ar[11] = "December";
// return name of specified month (parameter)
return ar[month];
}
setCal()函數(shù)是主模塊,我們?cè)谀_本的第一行調(diào)用它。該函數(shù)為當(dāng)天(now)、和每月的第
一天(firstDayInstance)建立一個(gè)Date對(duì)象。用這些對(duì)象,setCal()函數(shù)解析出關(guān)于一個(gè)月的第
一天、當(dāng)日,和最后一天的所有信息。
function setCal() {
// standard time attributes
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var monthName = getMonthName(month);
var date = now.getDate();
now = null;
// create instance of first day of month, and extract the day on which it occurs
var firstDayInstance = new Date(year, month, 1);
var firstDay = firstDayInstance.getDay();
firstDayInstance = null;
// number of days in current month
var days = getDays(month, year);
// call function to draw calendar
drawCal(firstDay + 1, days, date, monthName, year);
}