翻看Js关于Object的理解,找到了这么两个网站,帮助颇深
于是手动试了试赛迪网的例子,以便加深理解。
后面有一个日期的例子,Calendar.js 但是写了前半部分后,因为懒而且全是document.write输出
就拷贝了过来,可是结果运行不了,出错。
搜之,无果。后细细观察,MD原来是全角半角的问题。
也不知道为什么这么多人没有敲它的程序的。
现将其程序呈现到此,并且加了注释。以待记之。
如果要转载,请写明出处:opensuse
<script type="text/javascript">
Calendar = function(month,year)
{
this.days = new Array("周日","周一","周二","周三","周四","周五","周六");
this.months = new Array("一月","二月","三月","四月","五月","六月","七月","八月",
"九月","十月","十一月","十二月");
this.totalDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
//这里要将月份减1,因为JS Date的月份是从0-11
this.month = month-1;
this.year = year;
//这里是要判断闰年滴,如果润年,则二月份的最后一天是29号
if (this.year % 4 == 0 || (this.year % 100 == 0 && this.year % 4 != 0))
{
this.totalDays[1] = 29;
}
this.rowCount = 0;
this.display = Display;
this.display();
}
Display = function()
{
//获得给定年月的日期 具体参数含义参照
//http://www.fzs8.net/Java/JavaScript/2007-06-21/6874.html
obj = new Date(this.year, this.month, 1);
//得到给定月份的第一天是星期几返回值是 0(周日) 到 6(周六) 之间的一个整数。
//W3School上写错了
this.firstDayOfMonth = obj.getDay();
//这两句没有用到。但是其作用是得到给定月份最后一天是星期几
obj.setDate(31);
this.lastDayOfMonth = obj.getDay();
document.write("<table border=0 cellpadding=2 cellspacing=5>");
document.write("<tr><td colspan=7 align=center><font face='Arial' size=-1><b>" +
this.months[this.month] + " " + this.year + "</b></font></td></tr>");
document.write("<tr>");
//输出“周日,周一,。。。周六”
for(i=0;i<this.days.length;i++)
{
document.write("<td><font face=Arial size=-2>"+this.days[i] +
"</font></td>") ;
}
document.write("</tr>");
//循环,看第一天是星期几来决定输出几列空值,如星期三,则周日,周一,
//周二都是空的,这里一定是<=号
//rowCount即是输出了多少列
document.write("<tr>");
for (x=1; x<=this.firstDayOfMonth; x++)
{
this.rowCount++;
document.write("<td><font face=Arial size=-2> </font></td>");
}
//每个月都是从一号开始的
this.dayCount=1;
while (this.dayCount <= this.totalDays[this.month])
{
//七天了当然要换行
if (this.rowCount % 7 == 0)
{
document.write("</tr>\n<tr>");
}
//输出日期啊,这里dayCount要++因为用的是WHILE方法,还有这个rowCount也要++
document.write("<td align=center><font face=Arial size=-1>" + this.dayCount
+ "</font></td>");
this.dayCount++;
this.rowCount++;
}
document.write("</tr></table>");
}
//调用输出实例
bj1 = new Calendar(11, 2008);
</script>
了以自娱。
本文介绍了一个使用JavaScript实现的日历程序,通过构造函数和方法展示指定月份的详细日期。该程序考虑了不同月份天数和闰年的特殊情况。
1934

被折叠的 条评论
为什么被折叠?



