HTML組件(HTML COMPONENTS)之四

來源:互聯網
上載者:User
===編寫日曆一===

  當calendar.html調用 MYCAL:CALENDAR,當月的日曆將會顯示在頁面中,函數setCal()是主要程式段,它初始化一些變數並調用drawCal()函數。我們也使用了三個別的函數:getMonthName()、 getDays() 和 leapYear()。讓我們從最後一個函數開始:

  getDays()函數接收哪月值和哪年值,並且建立一個有12個元素的數組,用來存放每月的天數,哪一年用來決定是不是閏年,在閏年中二月是29天,而不是閏年是28天。該函數返回指定月份的天數。

以下是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]; } 如果指定的年數可以被4整除,那麼leapYear()函數將返回“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()函數返回指定月份的名字: 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()函數是主模組,我們在指令碼的第一行調用它。該函數為當天(now)、和每月的第一天(firstDayInstance)建立一個Date對象。用這些對象,setCal()函數解析出關於一個月的第一天、當日,和最後一天的所有資訊。 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); }

以上就是HTML組件(HTML COMPONENTS)之四的內容,更多相關文章請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.