显示任何月/年Javascript日历

时间:2017-11-01 01:20:18

标签: javascript html css

我正在尝试使用HTML和JavaScript编写日历,允许用户选择给定年份中的任何月份进行查看。我设置了一个HMTL表单,其中有一个下拉列表可以选择月份,输入框可以输入所需的年份。还有按钮编码在月份和年份之间切换,同时显示当前月份。所有这些都有效,我能够让日历显示在当月。

我正在努力解决的问题是,当我点击切换按钮或选择所需的月/年并按下显示按钮时,日历显示会发生变化。我不知道如何让我的日历以它应该的方式工作。

HTML

<!DOCTYPE html>
<html>
  <head>
    <script src = "CalendarFunction.js"></script>
    <link href="Calendartest.css" rel="stylesheet"/>
  </head>
  <body onLoad = "CreateCalendar()">
    <form name = "DateControl" id = "DateControl" onSubmit = "return false;" method = "post">
      <!--Select the month and year to be shown -->
      <select name = "Month" id = "month" onChange = "ChooseDate()">
        <option value="January" id="January">January</option>
        <option value="February" id="February">February</option>
        <option value="March" id="March">March</option>
        <option value="April" id="April">April</option>
        <option value="May" id="May">May</option>
        <option value="June" id="June">June</option>
        <option value="July" id="July" >July</option>
        <option value="August" id="August">August</option>
        <option value="September" id="September">September</option>
        <option value="October" id="October" >October</option>
        <option value="November" id="November">November</option>
        <option value="December" id="December">December</option>
      </select>
      <input name = "Year" type = "text" maxlength = "4">
      <!-- Show Calendar -->
      <input type = "button" name = "create" value = "Show" onClick = "ChooseDate()">
      </TD>
      </TR>
      <!--Toggle between the months of the year -->
      <input type = "button" name = "previousYear" value = "<Year" onClick = "PreviousYear()">
      <input type = "button" name = "previousMonth" value = "<Month" onClick = "PreviousMonth()">
      <input type = "button" name = "current" value = "Current" onClick = "SetDate()">
      <input type = "button" name = "nextYear" value = ">Year" onClick = "NextYear()">
      <input type = "button" name = "nextMonth" value = ">Month"onClick = "NextMonth()" >
    </form>
    <div id = "calendar"></div>
  </body>
</html>

JS

function SetDate() {
    var now = new Date();
    var day = now.getDate();
    var month = now.getMonth();
    var year = now.getFullYear();
    this.focusDay = day;
    document.DateControl.Month.selectedIndex = month;
    document.DateControl.Year.value = year;
    displayCalendar(month, year);
}

function isFourDigitYear(year) {
    if (year.length != 4) {
        alert("Sorry, the year must be four-digits in length.");
        document.DateControl.Year.select();
        document.DateControl.Year.focus();
    } else {
        return true;
    }
}

function ChooseDate() {
    var year = document.DateControl.Year.value;
    if (isFourDigitYear(year)) {
        var day = 0;
        var month = document.DateControl.Month.selectedIndex;
        displayCalendar(month, year);
    }
}

function PreviousYear() {
    var year = document.DateControl.Year.value;
    if (isFourDigitYear(year)) {
        var day = 0;
        var month = document.DateControl.Month.selectedIndex;
        year--;
        document.DateControl.Year.value = year;
        displayCalendar(month, year);
    }
}

function PreviousMonth() {
    var year = document.DateControl.Year.value;
    if (isFourDigitYear(year)) {
        var day = 0;
        var month = document.DateControl.Month.selectedIndex;
        if (month == 0) {
            month = 11;
            if (year > 1000) {
                year--;
                document.DateControl.Year.value = year;
            }
        } else {
            month--;
        }
        document.DateControl.Month.selectedIndex = month;
        displayCalendar(month, year);
    }
}

function NextMonth() {
    var year = document.DateControl.Year.value;
    if (isFourDigitYear(year)) {
        var day = 0;
        var month = document.DateControl.Month.selectedIndex;
        if (month == 11) {
            month = 0;
            year++;
            document.DateControl.Year.value = year;
        } else {
            month++;
        }
        document.DateControl.Month.selectedIndex = month;
        displayCalendar(month, year);
    }
}

function NextYear() {
    var year = document.DateControl.Year.value;
    if (isFourDigitYear(year)) {
        var day = 0;
        var month = document.DateControl.Month.selectedIndex;
        year++;
        document.DateControl.Year.value = year;
        displayCalendar(month, year);
    }
}

function CreateCalendar() {
    var htmlContent = "";
    var FebNumberOfDays = "";
    var counter = 1;
    var now = new Date();
    var day = now.getDate();
    var month = now.getMonth();
    var year = now.getFullYear();
    var nextMonth = month + 1;
    var prevMonth = month - 1;

    if (month == 1) {
        if ((year % 100 != 0) && (year % 4 == 0) || (year % 400 == 0)) {
            FebNumberOfDays = 29;
        } else {
            FebNumberOfDays = 28;
        }
    }

    var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var dayPerMonth = ["31", "" + FebNumberOfDays + "", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"];

    var nextDate = new Date(nextMonth + ' 1 ,' + year);
    var weekdays = nextDate.getDay();
    var weekdays2 = weekdays;
    var numOfDays = dayPerMonth[month];

    while (weekdays > 0) {
        htmlContent += "<td class='monthPre'></td>";
        weekdays--;
    }

    while (counter <= numOfDays) {

        if (weekdays2 > 6) {
            weekdays2 = 0;
            htmlContent += "</tr><tr>";
        }

        if (counter == day) {
            htmlContent += "<td class='dayNow'>" + counter + "</td>";
        } else {
            htmlContent += "<td class='monthNow'>" + counter + "</td>";
        }

        weekdays2++;
        counter++;
    }

    var calendarBody = "<table class='calendar'> <tr class='monthNow'><th colspan='7'>" +
        monthNames[month] + " " + year + "</th></tr>";

    calendarBody += "<tr class='dayNames'>  <td>Sun</td>  <td>Mon</td> <td>Tues</td>" +
        "<td>Wed</td> <td>Thurs</td> <td>Fri</td> <td>Sat</td> </tr>";
    calendarBody += "<tr>";
    calendarBody += htmlContent;
    calendarBody += "</tr></table>";
    document.getElementById("calendar").innerHTML = calendarBody;
}

CSS

.monthPre{
    color: gray;
    text-align: center;
}
.monthNow{
    color: blue;
    text-align: center;
}
.dayNow{
    border: 2px solid black;
    color: #FF0;
    text-align: center;
}
.calendar td{
    htmlContent: 2px;
    border: 2px solid black;
    width: 140px;
    height: 140px;
}
.monthNow th{
    background-color: #000000;
    color: #FFFFFF;
    text-align: center;

}
.dayNames{
    background: #0FF000;
    color: #FFFFFF;
    text-align: center;

}

0 个答案:

没有答案