javascript|控制項|日曆
該日曆控制項採用js編寫,來自MS的 http://expo.live.com,本站整理,希望對大家有用。
本程式中需要的兩個圖片:
http://expo.live.com/http://www.webjx.com/htmldata/2006-04-04/Images/leftArrow.gif
http://expo.live.com/http://www.webjx.com/htmldata/2006-04-04/Images/rightArrow.gif
Global.css
td.negDate a
{
color: #808080;
}
.calendar
{
padding-left: 4px;
padding-right: 4px;
}
.calendar table
{
border-bottom: solid 1px #366AB3;
text-align: center;
padding: 0;
}
.calendar table a
{
color: black;
}
.calendar td {
padding: 4px;
font-size: 8pt;
font-weight: lighter;
}
.calendarhd
{
color: #366AB3;
text-transform: uppercase;
}
.calendarDays
{
border-bottom: solid 1px;
color: #808080;
padding: 0;
margin: 0;
}
.selectedDate {
background: orange;
}
.selectedDate a {
color: white !important;
}
Global.js
var calendar_DayNames = new Array("日","一","二","三","四","五","六");
var calendar_MonthNames = new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
function showCalendar(targetID, targetURL, dMonth, dYear, dDay)
{
if(dMonth < 0)
{
dMonth = 11;
dYear--;
}
if(dMonth > 11)
{
dMonth = 0;
dYear++;
}
var dNow = new Date();
if(dDay == 90 && dMonth == dNow.getMonth() && dYear == dNow.getYear()) dDay = dNow.getDate();
var dCurFD = new Date(dYear, dMonth, 1);
var dGivenDate = new Date(dYear, dMonth, dDay);
var dCurrentMonthFirstSunday = dCurFD.getDay();
var dCurrentViewDate = new Date(dCurFD);
var iDays = 0;
var weekMilliseconds = 1000*60*60*24*7;
if(dCurrentMonthFirstSunday > 0) iDays = -dCurrentMonthFirstSunday;
var fTrgt = targetURL;
while (fTrgt.indexOf("'") >= 0) fTrgt = fTrgt.replace("'", "`");
while (fTrgt.indexOf("`") >= 0) fTrgt = fTrgt.replace("`", "\\'");
var oBuf = '<TABLE BORDER="0" WIDTH="100%" CELLSPACING="0" CELLPADDING="0">';
oBuf += '<TR><TD CLASS="calendarhd"><A HREF="javascript:showCalendar(\'' + targetID + "','" + fTrgt + "'," + (dMonth - 1) + ',' + dYear + ',90);"><img src="http://www.webjx.com/htmldata/2006-04-04/Images/leftArrow.gif" border=0 /></A></TD>';
oBuf += '<TD COLSPAN="5" CLASS="calendarhd">' + (calendar_MonthNames[dMonth]) + ' ' + dYear + '</TD>';
oBuf += '<TD CLASS="calendarhd"><A HREF="javascript:showCalendar(\'' + targetID + "','" + fTrgt + "'," + (dMonth + 1) + ',' + dYear + ',90);"><img src="http://www.webjx.com/htmldata/2006-04-04/Images/rightArrow.gif" border=0 /></A></TD></TR>';
oBuf += '<TR style="calendarhdrow">';
for(var e=0; e < 7; e++) oBuf += '<TD CLASS="calendarDays">' + calendar_DayNames[e] + '</TD>';
oBuf += '</TR>';
var nextMonthFirstDay = new Date(dYear, dMonth+1, 1);
var IsNextMonthFirstDaySunday = nextMonthFirstDay.getDay()==0;
for(var iRows=0; iRows < 6; iRows++)
{
dCurrentMonthDate = new Date(dCurFD.getFullYear(), dCurFD.getMonth(), dCurFD.getDate() + iDays);
if(1==1)
{
if(iRows==5 && dCurrentMonthDate.getMonth() > dCurFD.getMonth()) continue;
if(iRows==5 && IsNextMonthFirstDaySunday) continue;
oBuf += '<TR CLASS="calendarContent">';
for(var iCols=0; iCols < 7; iCols++)
{
dCurrentViewDate = new Date(dCurFD.getFullYear(), dCurFD.getMonth(), dCurFD.getDate() + iDays);
if((dGivenDate.toDateString() == dCurrentViewDate.toDateString()) && (dDay != 90))
{
oBuf += '<TD CLASS="selectedDate">';
}
else
{
if(dCurrentViewDate.getMonth() != dMonth || iDays < 0)
{
oBuf += '<TD CLASS="negDate">';
}
else
{
oBuf += '<TD CLASS="posDate">';
}
}
var args;
if (targetURL.substr(0,11) == 'javascript:')
{
args = dCurrentViewDate.valueOf() + ")";
}
else
{
var sep = targetURL.indexOf('?') < 0 ? '?' : '&';
args = sep + 'startDate=' + dCurrentViewDate.getFullYear()+ '-' + (dCurrentViewDate.getMonth()+1) + '-' +dCurrentViewDate.getDate() ;
}
oBuf += '<A HREF="' + targetURL + args + '">' + dCurrentViewDate.getDate() + '</A></TD>';
iDays++;
}
oBuf += '</TR>';
}
}
oBuf += '</TABLE>';
document.getElementById(targetID).innerHTML = oBuf;
}
調用
<LINK href="Global.css" rel="stylesheet">
<script type="text/javascript" src="global.js"></script>
<div id="calendar1" class="calendar"></div>
<script type="text/javascript">
var dNow = new Date();
showCalendar('calendar1', FileName.aspx', dNow.getMonth(), dNow.getFullYear(), dNow.getDate())
</script>