📄 simplecalendar.js
字号:
var timeoutDelay = 2000;
var g_startDay = 0; // 0=sunday, 1=monday
var timeoutId = false;
function Browser(){
this.dom = document.getElementById?1:0;
this.ie4 = (document.all && !this.dom)?1:0;
this.ns4 = (document.layers && !this.dom)?1:0;
this.ns6 = (this.dom && !document.all)?1:0;
this.ie5 = (this.dom && document.all)?1:0;
this.ok = this.dom || this.ie4 || this.ns4;
this.platform = navigator.platform;
}
var browser = new Browser();
if (browser.dom || browser.ie4){
document.writeln('<style>');
document.writeln('#container {');
document.writeln('position : absolute;');
document.writeln('left : 100px;');
document.writeln('top : 100px;');
document.writeln('width : 124px;');;
browser.platform=='Win32'?height=140:height=145;
document.writeln('height : ' + height +'px;');
document.writeln('clip:rect(0px 124px ' + height + 'px 0px);');
document.writeln('visibility : hidden;');
document.writeln('background-color : #ffffff');
document.writeln('}');
document.writeln('</style>')
document.write('<div id="container"');
if (timeoutDelay) document.write(' onmouseout="calendarTimeout();" onmouseover="if (timeoutId) clearTimeout(timeoutId);"');
document.write('></div>');
}
var g_Calendar;
function calendarTimeout(){
if (browser.ie4 || browser.ie5){
if (window.event.srcElement && window.event.srcElement.name!='month') timeoutId=setTimeout('g_Calendar.hide();',timeoutDelay);
}
if (browser.ns6 || browser.ns4){
timeoutId=setTimeout('g_Calendar.hide();',timeoutDelay);
}
}
function Calendar(){
g_Calendar = this;
this.daysOfWeek = new Array("Su","Mo","Tu","We","Th","Fr","Sa");
this.months = new Array(" 01 Jan"," 02 Feb"," 03 Mar"," 04 Apr"," 05 May"," 06 Jun"," 07 Jul"," 08 Aug"," 09 Sep"," 10 Oct"," 11 Nov"," 12 Dec");
this.daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if (browser.ns4){
var tmpLayer = new Layer(127);
if (timeoutDelay){
tmpLayer.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
tmpLayer.onmouseover = function(event) { if (timeoutId) clearTimeout(timeoutId); };
tmpLayer.onmouseout = function(event) { timeoutId=setTimeout('g_Calendar.hide()',timeoutDelay);};
}
tmpLayer.x = 100;
tmpLayer.y = 100;
tmpLayer.bgColor = "#ffffff";
}
if (browser.dom || browser.ie4){
var tmpLayer = browser.dom?document.getElementById('container'):document.all.container;
}
this.containerLayer = tmpLayer;
if (browser.ns4 && browser.platform=='Win32') {
this.containerLayer.clip.height=134;
this.containerLayer.clip.width=127;
}
}
Calendar.prototype.getFirstDOM = function() {
var thedate = new Date();
thedate.setDate(1);
thedate.setMonth(this.month);
thedate.setFullYear(this.year);
return thedate.getDay();
}
Calendar.prototype.getDaysInMonth = function (){
if (this.month!=1) {
return this.daysInMonth[this.month]
}
else {
if (Date.isLeapYear(this.year)) {
return 29;
}
else {
return 28;
}
}
}
Calendar.prototype.buildString = function(){
var tmpStr = '<form onSubmit="this.year.blur();return false;"><table width="100%" border="0" cellspacing="0" cellpadding="2" class="calBorderColor"><tr><td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="1" class="calBgColor">';
tmpStr += '<tr>';
tmpStr += '<td width="60%" class="cal" align="left">';
if (this.hasDropDown) {
tmpStr += '<select class="month" name="month" onchange="g_Calendar.selectChange();">';
for (var i=0;i<this.months.length;i++){
tmpStr += '<option value="' + i + '"'
if (i == this.month) tmpStr += ' selected';
tmpStr += '>' + this.months[i] + '</option>';
}
tmpStr += '</select>';
} else {
tmpStr += '<table border="0" cellspacing="0" cellpadding="0"><tr><td class="cal" width="100%" align="center">' + this.months[this.month] + '</td></tr></table>';
}
tmpStr += '</td>';
tmpStr += '<td width="40%" align="right" class="cal">';
if (this.hasDropDown) {
tmpStr += '<select class="year" name="year" onchange="g_Calendar.inputChange();">';
for (var i=this.year;i>=this.year-1;i--){
tmpStr += '<option value="' + i + '"'
tmpStr += '>' + i + '</option>';
}
tmpStr += '</select>';
} else {
tmpStr += '<table border="0" cellspacing="0" cellpadding="0"><tr><td class="cal" width="100%" align="center">' + this.year + '</td></tr></table>'
}
tmpStr += '</td>';
tmpStr += '</tr>';
tmpStr += '</table>';
var iCount = 1;
var iFirstDOM = (7+this.getFirstDOM()-g_startDay)%7; // to prevent calling it in a loop
var iDaysInMonth = this.getDaysInMonth(); // to prevent calling it in a loop
tmpStr += '<table width="100%" border="0" cellspacing="0" cellpadding="1" class="calBgColor">';
tmpStr += '<tr>';
for (var i=0;i<7;i++){
tmpStr += '<td align="center" class="calDaysColor">' + this.daysOfWeek[(g_startDay+i)%7] + '</td>';
}
tmpStr += '</tr>';
var tmpFrom = parseInt('' + this.dateFromYear + this.dateFromMonth + this.dateFromDay,10);
var tmpTo = parseInt('' + this.dateToYear + this.dateToMonth + this.dateToDay,10);
var tmpCompare;
for (var j=1;j<=6;j++){
tmpStr += '<tr>';
for (var i=1;i<=7;i++){
tmpStr += '<td width="16" align="center" '
if ( (7*(j-1) + i)>=iFirstDOM+1 && iCount <= iDaysInMonth){
if (iCount==this.day && this.year==this.oYear && this.month==this.oMonth) tmpStr += 'class="calHighlightColor"';
else {
if (i==7-g_startDay || i==((7-g_startDay)%7)+1) tmpStr += 'class="calWeekend"';
else tmpStr += 'class="cal"';
}
tmpStr += '>';
tmpCompare = parseInt('' + this.year + padZero(this.month) + padZero(iCount),10);
if (tmpCompare >= tmpFrom && tmpCompare <= tmpTo) {
tmpStr += '<a class="cal" href="javascript: g_Calendar.clickDay(' + iCount + ');">' + iCount + '</a>';
} else {
tmpStr += '<span class="disabled">' + iCount + '</span>';
}
iCount++;
} else {
if (i==7-g_startDay || i==((7-g_startDay)%7)+1) tmpStr += 'class="calWeekend"'; else tmpStr +='class="cal"';
tmpStr += '> ';
}
tmpStr += '</td>'
}
tmpStr += '</tr>'
}
tmpStr += '</table></td></tr></table></form>'
return tmpStr;
}
Calendar.prototype.selectChange = function(){
this.month = browser.ns6?this.containerLayer.ownerDocument.forms[0].month.selectedIndex:this.containerLayer.document.forms[0].month.selectedIndex;
this.writeString(this.buildString());
}
Calendar.prototype.inputChange = function(){
var tmp = browser.ns6?this.containerLayer.ownerDocument.forms[0].year:this.containerLayer.document.forms[0].year;
if (tmp.value >=1900 || tmp.value <=2100){
this.year = tmp.value;
this.writeString(this.buildString());
} else {
tmp.value = this.year;
}
}
Calendar.prototype.changeYear = function(incr){
(incr==1)?this.year++:this.year--;
this.writeString(this.buildString());
}
Calendar.prototype.changeMonth = function(incr){
if (this.month==11 && incr==1){
this.month = 0;
this.year++;
} else {
if (this.month==0 && incr==-1){
this.month = 11;
this.year--;
} else {
(incr==1)?this.month++:this.month--;
}
}
this.writeString(this.buildString());
}
Calendar.prototype.clickDay = function(day){
var tmp = eval('document.' + this.target);
tmp.value = this.formatDateAsString(day,this.month,this.year);
if (browser.ns4) this.containerLayer.hidden=true;
if (browser.dom || browser.ie4){
this.containerLayer.style.visibility='hidden';
try{
document.all.wtype.style.visibility='visible';
}catch(e){}
try{
document.all.pay_type.style.visibility='visible';
}catch(e){}
try{
document.all.report_kind.style.visibility='visible';
}catch(e){}
try{
document.all.cid.style.visibility='visible';
}catch(e){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -