我的javascript函数有什么问题?

时间:2013-07-14 19:21:38

标签: javascript function

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

这是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>");


    }

3 个答案:

答案 0 :(得分:0)

也许放:

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

在剧本里面。

答案 1 :(得分: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>

......应该会有更好的结果。

答案 2 :(得分:0)

您必须将代码放在<script>标记内,或通过<script src="...">加载。