windows7怎么弄内存:JavaScript日历显示异常的问题,麻烦大家了!

来源:百度文库 编辑:高考问答 时间:2024/05/04 13:07:10
以下是JavaScript的代码,我显示出来的结果在日历下方多了“undefined”这一句,请问怎么可以将其去除,谢谢!

function montharr(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11)
{
this[0] = m0;
this[1] = m1;
this[2] = m2;
this[3] = m3;
this[4] = m4;
this[5] = m5;
this[6] = m6;
this[7] = m7;
this[8] = m8;
this[9] = m9;
this[10] = m10;
this[11] = m11;
}

function calendar()
{
var monthNames = "JanFebMarAprMayJunJulAugSepOctNovDec";
var today = new Date();
var thisDay;
var monthDays = new montharr(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

year = today.getYear();
thisDay = today.getDate();

// do the classic leap year calculation
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
monthDays[1] = 29;

// figure out how many days this month will have...
nDays = monthDays[today.getMonth()];

// and go back to the first day of the month...
firstDay = today;
firstDay.setDate(1);
// and figure out which day of the week it hits...
startDay = firstDay.getDay();

document.writeln("<CENTER>");
document.write("<TABLE border=1>");
document.write("<TR><TH COLSPAN=7>");
document.write(monthNames.substring(today.getMonth() * 3,(today.getMonth() + 1) * 3));
document.write(". ");
document.write(year);
document.write("<TR><TH>Sun<TH>Mon<TH>Tue<TH>Wed<TH>Thu<TH>Fri<TH>Sat");

// now write the blanks at the beginning of the calendar
document.write("<TR>");
column = 0;
for (i=0; i<startDay; i++)
{
document.write("<TD>");
column++;
}

for (i=1; i<=nDays; i++)
{
document.write("<TD>");
if (i == thisDay)
document.write("<FONT COLOR=\"#FF0000\">")
document.write(i);
if (i == thisDay)
document.write("</FONT>")
column++;
if (column == 7)
{
document.write("<TR>"); // start a new row
column = 0;
}
}
document.write("</TABLE>");
document.writeln("</CENTER>");
}
document.write(calendar());
document.write("<HR>");

你在document.write()里调用函数,最后会把函数的返回值写在页面上,而你没有声明这个返回值,所以就显示一个undefined,
有两种解决方法
1.把要显示的字符串全部赋值给一个全局字符串,然后
document.write()这个字符串
2.在函数最后加一句 return "",声明一个为空字符串的返回值,这也可以

倒数第二行的document.write(calendar());
改为:calendar();