| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 740 人关注过本帖
标题:一个简单的日历
取消只看楼主 加入收藏
vicki1986
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2005-11-29
收藏
 问题点数:0 回复次数:2 
一个简单的日历

以下是程序代码:运行过是正确的显示日历的~大家来看看~有点问题想请教~

<html>
<head>
<title>JavaScript日历</title>
<SCRIPT LANGUAGE = javascript>
var now = new Date();
var CurrentMonth = now.getMonth();
var CurrentDayOfMonth = now.getDate();
var CurrentYear = now.getYear();
var objPrevElement = new Object();


function fBuildCal(iYear, iMonth, iDayStyle)
{
var aMonth = new Array();
aMonth[0] = new Array(7);
aMonth[1] = new Array(7);
aMonth[2] = new Array(7);
aMonth[3] = new Array(7);
aMonth[4] = new Array(7);
aMonth[5] = new Array(7);
aMonth[6] = new Array(7);
var dCalDate = new Date(iYear, iMonth-1, 1);
var iDayOfFirst = dCalDate.getDay();
var iDaysInMonth = fGetDaysInMonth(iMonth, iYear);
var iVarDate = 1;
var i, d, w;
aMonth[0][0] = "星期日";
aMonth[0][1] = "星期一";
aMonth[0][2] = "星期二";
aMonth[0][3] = "星期三";
aMonth[0][4] = "星期四";
aMonth[0][5] = "星期五";
aMonth[0][6] = "星期六";
for (d = iDayOfFirst; d < 7; d++)
{
aMonth[1][d] = iVarDate;
iVarDate++;
}
for (w = 2; w < 7; w++)
for (d = 0; d < 7; d++)
if (iVarDate <= iDaysInMonth)
{
aMonth[w][d] = iVarDate;
iVarDate++;
}
return aMonth;
}

function fGetDaysInMonth(iMonth, iYear)
{
var dPrevDate = new Date(iYear, iMonth, 0);
return dPrevDate.getDate();
}

function setCalendar(iYear, iMonth, iCellWidth, iCellHeight, sDateTextSize, sDateTextWeight)
{
var myMonth;
myMonth = fBuildCal(iYear, iMonth);
document.write('<p align="center">');
document.write('<table border="1">');
document.write("<tr>");
var insert = "<td align='center' style='FONT-FAMILY:Arial;FONT-SIZE:12px;FONT-WEIGHT: bold'>"
document.write(insert + myMonth[0][0] + "</td>");
document.write(insert + myMonth[0][1] + "</td>");
document.write(insert + myMonth[0][2] + "</td>");
document.write(insert + myMonth[0][3] + "</td>");
document.write(insert + myMonth[0][4] + "</td>");
document.write(insert + myMonth[0][5] + "</td>");
document.write(insert + myMonth[0][6] + "</td>");
document.write("</tr>");
for (w = 1; w < 7; w++)
{
document.write("<tr>")
for (d = 0; d < 7; d++)
{
document.write("<td align='left' valign='top'");
document.write("width='" + iCellWidth + "'height='" + iCellHeight);
document.write("'id=calCell style='CURSOR:Hand'");
document.write("onMouseOver='fToggleColor(this)'");
document.write("onMouseOut='fToggleColor(this)'");
document.write("onclick=fSetSelectedDay(this)>");
if (!isNaN(myMonth[w][d]))
{
document.write("<font id=calDateText onMouseOver='fToggleColor(this)'");
document.write("style='CURSOR:Hand;FONT-FAMILY:Arial;");
document.write("FONT-SIZE:" + sDateTextSize);
document.write(";FONT-WEIGHT:" + sDateTextWeight + "'");
document.write("onMouseOut='fToggleColor(this)'");
document.write("onclick=fSetSelectedDay(this)>");
document.write(myMonth[w][d] + "</font>");
}
else
{
document.write("<font id=calDateText onMouseOver='fToggleColor(this)'");
document.write("style='CURSOR:Hand;FONT-FAMILY:Arial;");
document.write("FONT-SIZE:" + sDateTextSize);
document.write(";FONT-WEIGHT:" + sDateTextWeight + "'");
document.write("onMouseOut='fToggleColor(this)'");
document.write("onclick='fSetSelectedDay(this)'> </font>");
}
document.write("</td>")
}
document.write("</tr>");
}
document.write("</table>")
}

function update(iYear, iMonth)
{
myMonth = fBuildCal(iYear, iMonth);
objPrevElement.bgColor = "";
document.all.calSelectenow.value = "";
for (w = 1; w < 7; w++)
{
for (d = 0; d < 7; d++)
{
if (!isNaN(myMonth[w][d]))
calDateText[((7*w)+d)-7].innerText = myMonth[w][d];
else
calDateText[((7*w)+d)-7].innerText = " ";
}
}
}

function fToggleColor(myElement)
{
var toggleColor = "#ff0000";
if (myElement.id == "calDateText")
if (myElement.color == toggleColor)
myElement.color = "";
else
myElement.color = toggleColor;
else if (myElement.id == "calCell")
for (var i in myElement.children)
if (myElement.children[i].id == "calDateText")
if (myElement.children[i].color == toggleColor)
myElement.children[i].color = "";
else
myElement.children[i].color = toggleColor;
}

function fSetSelectedDay(myElement)
{
if (myElement.id == "calCell")
{
if (!isNaN(parseInt(myElement.children["calDateText"].innerText)))
{
myElement.bgColor = "#c0c0c0";
objPrevElement.bgColor = "";
document.all.calSelectenow.value=parseInt(myElement.children ["calDateText"] .innerText);
objPrevElement = myElement;
}
}
}
</script>
</head>
<body>
<p align="center">
<font size="6" face="宋体">Javascript日历</font>
<form name="MyCalendar" method="post" action="">
<input type="hidden" name="calSelectenow" value="">
<div align="center">
<table border="1" width="265" height="51">
<tr>
<td width="255" height="23">
<p align="left">
<select name="SetYear"
onchange='update(MyCalendar.SetYear.value, MyCalendar.SetMonth.value)'>
<option value="1998">1998年</option>
<option value="1999">1999年</option>
<option value="2000">2000年</option>
<option value="2001">2001年</option>
<option value="2002">2002年</option>
<option value="2003">2003年</option>
<option value="2004">2004年</option>
</select>
<select name="SetMonth"
onchange='update(MyCalendar.SetYear.value, MyCalendar.SetMonth.value)'>
<option value="1">一月</option>
<option value="2">二月</option>
<option value="3">三月</option>
<option value="4">四月</option>
<option value="5">五月</option>
<option value="6">六月</option>
<option value="7">七月</option>
<option value="8">八月</option>
<option value="9">九月</option>
<option value="10">十月</option>
<option value="11">十一月</option>
<option value="12">十二月</option>
</select> </p>
</td> </tr>
<tr>
<td width="255" height="16">
<script language="JavaScript">
var CurrentDate = new Date();
setCalendar(CurrentDate.getYear(), CurrentDate.getMonth()+1, 30, 30, "12px", "bold");
var CurrentDate = new Date();
MyCalendar.SetMonth.options[CurrentDate.getMonth()].selected = true;
for (i = 0; i < MyCalendar.SetYear.length; i++)
if (MyCalendar.SetYear.options[i].value == CurrentDate.getYear())
MyCalendar.SetYear.options[i].selected = true;
</script>
</td>
</tr>
</table>
</div> </form>
</body>
</html>

有点问题不解~就是以下这段代码~
function fGetDaysInMonth(iMonth, iYear)
{
var dPrevDate = new Date(iYear, iMonth, 0);
return dPrevDate.getDate();
}
这段代码为什么计算的是iYear年iMonth月的上一个月的最后一天是多少号?
本人理解是这段代码应该是计算iMonth月的最后一天是多少号~
谢谢!!

搜索更多相关主题的帖子: var 日历 new aMonth Array 
2006-01-21 18:22
vicki1986
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2005-11-29
收藏
得分:0 
不好意思~补充一点~书本上是说计算iMonth月的上一个月的最后一天是多少号~
2006-01-21 18:24
vicki1986
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2005-11-29
收藏
得分:0 
顶~谢谢了~
2006-01-25 15:04
快速回复:一个简单的日历
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038318 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved