1

我正在编写此脚本以显示我的日历,错误控制台显示“未定义日历”。我无法弄清楚它有什么问题。

这是 John WHS 要求的 .js 页面的代码

    function calendar()
{

 var calDate = new Date("July 6, 2015");
    document.write("<table id = 'calendar_table'>");
writeCalTitle(calDate); 
    writeDayNames();
writeCalDays(calDate);

    document.write("</table>");

}


    //function for the title of the calendar
    //the parameter is a date object
    function writeCalTitle(calendarDay)
    {

        // This is an array for the months
        var monthName = ["January","February","March","April","May","June","July","August"
        ,"September","October","November","December"]

        var thisMonth = calendarDay.getMonth();
        var thisYear = caalendarDay.getFullYear();

        document.write("<tr>");
        document.write("<th id='calendar_head' colspan='7'>");
        document.write(monthName[month] + " " +year);

        document.write("</th>");
        document.write("</tr>");
    }

     function writeDayNames()
 {
    var dayName = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
    document.write("<tr>");
    // This is a for loop
    for(var i = 0; i<dayName.length; i++)
        {
        document.write("<th id='calendar_weekdays'>" + dayName[i]+"</th>");
        }

    document.write("</tr>");
 }


function daysInMonth(calendarDay)
{
var dayCount = [31,28,31,30,31,30,31,31,30,31,30,31];
var thisYear = calendarDay.getFullYear();
var thisMonth = calendarDay.getMonth();
if (thisYear % 4 == 0) 
    {

    if ((thisYear % 100 != 0 || (thisYear % 400 ==0 ))
    {
    dayCount[1] = 29;
    }
}
return dayCount[thisMonth];
}



function writeCalDays(calendarDay)
    {
    // says the starting day of the month
    var day = new Date(calendarDay.getFullYear(), calendarDay.getMonth(), 1 );
    var weekDay = day.getDay();

    // writes blank cells before the starting day
    document.write("<tr>");
    for (var i = 0; i < weekDay; i++)
        {
        document.write("<td></td>");
        }

    // writes cells for the days of the month
    var totalDays = dayIn Month(calendarDay);

    for (var i = 1; i<= totalDays; i++)
        {
        day.setDate(i);
        weekDay = day.getDay();
        }
    if (weekDay == 0) document.write("<tr>");
    document.write("<td class='calendar_dates'>" + i + "</td>");
    if (i == highlightDay) 
        {
        document.write("<td class='calendar_dates' id='calendar_today'>" + i + "</td>");
        }
        else
        {
        document.write("<td class='calendar_dates'>" + i + "</td>");
        }

    if (weekDay == 6) document.write("</tr>");


    }
4

3 回答 3

0

也许放:

function calendar(){
    var calDate = new Date("July 6, 2015");
    document.write("<table id = 'calendar_table'>");
    writeCalTitle(calDate); 
    writeDayNames();
    writeCalDays(calDate);
    document.write("</table>");
}

剧本里面。

于 2013-07-14T19:23:55.377 回答
0

函数声明也应该被视为脚本。所以...

<figure id="events">
<script type="text/javascript">

    function calendar()
    {

        var calDate = new Date("July 6, 2015");
        document.write("<table id = 'calendar_table'>");
        writeCalTitle(calDate); 
        writeDayNames();
        writeCalDays(calDate);

        document.write("</table>");

    }

    calendar();

</script>
</figure>

...应该给出更好的结果。

于 2013-07-14T19:24:23.940 回答
0

您必须在标签内有<script>代码,或通过<script src="...">.

于 2013-07-14T19:24:32.473 回答