2013-07-14 80 views
1

我正在處理此腳本以顯示我的日曆,錯誤控制檯顯示「日曆未定義」。我無法弄清楚它有什麼問題。我的JavaScript函數有什麼問題?

這是對的.js頁面的代碼由約翰·WHS

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


    } 
+0

該代碼在哪裏? – SLaks

+2

您的函數定義在'

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> 

...應該給出更好的結果。

+0

John WHS謝謝,改變了它......雖然沒有顯示日曆,但... – rikeerik

+0

首先,你打開一個表格,但不使用行,也不使用單元格。向我們展示'write *'函數的代碼,最重要的是:檢查頁面的源代碼。也許代碼**是**生成的,即使瀏覽器的解釋失敗了某些東西^^ –

+0

我在源代碼中尋找什麼? – rikeerik

0

您必須將代碼放在<script>標記內,或通過<script src="...">加載。

+0

我發現,當使用記事本++並試圖將'加載到HTML中。 – 2013-07-15 10:21:03