📄 simplecalendar.js
字号:
} 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'; } } Calendar.prototype.formatDateAsString = function(day, month, year){ var delim = eval('/\\' + this.dateDelim + '/g'); switch (this.dateFormat.replace(delim,"")){ case 'ddmmmyyyy': return padZero(day) + this.dateDelim + this.months[month].substr(0,3) + this.dateDelim + year; case 'ddmmyyyy': return padZero(day) + this.dateDelim + padZero(month+1) + this.dateDelim + year; case 'mmddyyyy': return padZero((month+1)) + this.dateDelim + padZero(day) + this.dateDelim + year; case 'yyyymmdd': return year + this.dateDelim + padZero(month+1) + this.dateDelim + padZero(day); case 'yyyymmdd': return year + this.dateDelim + padZero(month+1) + this.dateDelim + padZero(day); case 'yyyy-mm-dd': return year + this.dateDelim + padZero(month+1) + this.dateDelim + padZero(day); case 'yyyymmdd': return year + padZero(month+1) + padZero(day); default: alert('unsupported date format'); } } Calendar.prototype.writeString = function(str){ if (browser.ns4){ this.containerLayer.document.open(); this.containerLayer.document.write(str); this.containerLayer.document.close(); } if (browser.dom || browser.ie4){ this.containerLayer.innerHTML = str; } } Calendar.prototype.show = function(event, target, bHasDropDown, dateFormat, dateFrom, dateTo){ // calendar can restrict choices between 2 dates, if however no restrictions // are made, let them choose any date between 1900 and 3000 this.dateFrom = dateFrom || new Date(1900,0,1); this.dateFromDay = padZero(this.dateFrom.getDate()); this.dateFromMonth = padZero(this.dateFrom.getMonth()); this.dateFromYear = this.dateFrom.getFullYear(); this.dateTo = dateTo || new Date(3000,0,1); this.dateToDay = padZero(this.dateTo.getDate()); this.dateToMonth = padZero(this.dateTo.getMonth()); this.dateToYear = this.dateTo.getFullYear(); this.hasDropDown = bHasDropDown; this.dateFormat = dateFormat || 'dd-mmm-yyyy'; switch (this.dateFormat){ case 'dd-mmm-yyyy': case 'dd-mm-yyyy': case 'yyyy-mm-dd': this.dateDelim = '-'; break; case 'dd/mm/yyyy': case 'mm/dd/yyyy': case 'dd/mmm/yyyy': this.dateDelim = '/'; break; } if (browser.ns4) { if (!this.containerLayer.hidden) { this.containerLayer.hidden=true; return; } } if (browser.dom || browser.ie4){ if (this.containerLayer.style.visibility=='visible') { this.containerLayer.style.visibility='hidden'; return; } } if (browser.ie5 || browser.ie4){ var event = window.event; } if (browser.ns4){ this.containerLayer.x = event.x+10; this.containerLayer.y = event.y-5; } if (browser.ie5 || browser.ie4){ var obj = event.srcElement; x = 0; while (obj.offsetParent != null) { x += obj.offsetLeft; obj = obj.offsetParent; } x += obj.offsetLeft; y = 0; var obj = event.srcElement; while (obj.offsetParent != null) { y += obj.offsetTop; obj = obj.offsetParent; } y += obj.offsetTop; this.containerLayer.style.left = x; if (event.y>0)this.containerLayer.style.top = y+20; } if (browser.ns6){ this.containerLayer.style.left = event.pageX+10; this.containerLayer.style.top = event.pageY-5; } this.target = target; var tmp = eval('document.' + this.target); if (tmp && tmp.value && tmp.value.split(this.dateDelim).length==3 && tmp.value.indexOf('d')==-1){ var atmp = tmp.value.split(this.dateDelim) switch (this.dateFormat){ case 'dd-mmm-yyyy': case 'dd/mmm/yyyy': for (var i=0;i<this.months.length;i++){ if (atmp[1].toLowerCase()==this.months[i].substr(0,3).toLowerCase()){ this.month = this.oMonth = i; break; } } this.day = parseInt(atmp[0],10); this.year = this.oYear = parseInt(atmp[2],10); break; case 'dd/mm/yyyy': case 'dd-mm-yyyy': this.month = this.oMonth = parseInt(atmp[1]-1,10); this.day = parseInt(atmp[0],10); this.year = this.oYear = parseInt(atmp[2],10); break; case 'mm/dd/yyyy': case 'mm-dd-yyyy': this.month = this.oMonth = parseInt(atmp[0]-1,10); this.day = parseInt(atmp[1],10); this.year = this.oYear = parseInt(atmp[2],10); break; case 'yyyy-mm-dd': this.month = this.oMonth = parseInt(atmp[1]-1,10); this.day = parseInt(atmp[2],10); this.year = this.oYear = parseInt(atmp[0],10); break; } } else { // no date set, default to today var theDate = new Date(); this.year = this.oYear = theDate.getFullYear(); this.month = this.oMonth = theDate.getMonth(); this.day = this.oDay = theDate.getDate(); } this.writeString(this.buildString()); // and then show it! if (browser.ns4) { this.containerLayer.hidden=false; } if (browser.dom || browser.ie4){ this.containerLayer.style.visibility='visible'; } } Calendar.prototype.hide = function(){ if (browser.ns4) this.containerLayer.hidden = true; if (browser.dom || browser.ie4){ this.containerLayer.style.visibility='hidden'; } } function handleDocumentClick(e){ if (browser.ie4 || browser.ie5) e = window.event; if (browser.ns6){ var bTest = (e.pageX > parseInt(g_Calendar.containerLayer.style.left,10) && e.pageX < (parseInt(g_Calendar.containerLayer.style.left,10)+125) && e.pageY < (parseInt(g_Calendar.containerLayer.style.top,10)+125) && e.pageY > parseInt(g_Calendar.containerLayer.style.top,10)); if (e.target.name!='imgCalendar' && e.target.name!='month' && e.target.name!='year' && e.target.name!='calendar' && !bTest){ g_Calendar.hide(); } } if (browser.ie4 || browser.ie5){ // extra test to see if user clicked inside the calendar but not on a valid date, we don't want it to disappear in this case var bTest = (e.x > parseInt(g_Calendar.containerLayer.style.left,10) && e.x < (parseInt(g_Calendar.containerLayer.style.left,10)+125) && e.y < (parseInt(g_Calendar.containerLayer.style.top,10)+125) && e.y > parseInt(g_Calendar.containerLayer.style.top,10)); if (e.srcElement.name!='imgCalendar' && e.srcElement.name!='month' && e.srcElement.name!='year' && !bTest & typeof(e.srcElement)!='object'){ g_Calendar.hide(); } } if (browser.ns4) g_Calendar.hide(); } // utility function function padZero(num) { return ((num <= 9) ? ("0" + num) : num); } // Finally licked extending native date object; Date.isLeapYear = function(year){ if (year%4==0 && ((year%100!=0) || (year%400==0))) return true; else return false; } Date.daysInYear = function(year){ if (Date.isLeapYear(year)) return 366; else return 365;} var DAY = 1000*60*60*24; Date.prototype.addDays = function(num){ return new Date((num*DAY)+this.valueOf()); } // events capturing, careful you don't override this by setting something in the onload event of // the body tag window.onload=function(){ new Calendar(new Date()); if (browser.ns4){ if (typeof document.NSfix == 'undefined'){ document.NSfix = new Object(); document.NSfix.initWidth=window.innerWidth; document.NSfix.initHeight=window.innerHeight; } } } if (browser.ns4) window.onresize = function(){ if (document.NSfix.initWidth!=window.innerWidth || document.NSfix.initHeight!=window.innerHeight) window.location.reload(false); } // ns4 resize bug workaround window.document.onclick=handleDocumentClick; window.onerror = function(msg,url,line){ alert('******* an error has occurred ********' + '\n\nPlease check that' + '\n\n1)You have not added any code to the body onload event,' + '\nif you want to run something as well as the calendar initialisation' + '\ncode, add it to the onload event in the calendar library.' + '\n\n2)You have set the parameters correctly in the g_Calendar.show() method ' + '\n\nSee www.totallysmartit.com\\examples\\calendar\\simple.asp for examples' + '\n\n------------------------------------------------------' + '\nError details' + '\nText:' + msg + '\nurl:' + url + '\nline:' + line); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -