提示:您可以先修改部分代碼再運行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD> <TITLE> New Document </TITLE> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <style type="text/css"> body{font:12px Arial;} a:link,a:visited,a:active{color:#000;text-decoration:none;background:none;} a:hover{color:#fff;text-decoration:none;background:#000;} </style> </HEAD> <BODY> 一個通用的行事曆程式,IE6、Mozilal系列測試通過.歡迎使用<br/> <script type="text/javascript"> /***************** * *行事曆程式 *作者:marcian *首頁:www.marcian.net *2007.2.10 *特點:封裝,對外暴露一個方法來調用日曆,最大最小年份可調,日期可用性選擇.採用DOM方式建立日曆對象,頁面只需調用一個js檔案即可。 *使用方法:建立日曆對象,然後調用此對象的showCalendar()方法即可. *相容性:IE6,Mozilla系列測試通過 *歡迎交流. *自由使用,但請保留作者資訊。 *西曆演算法來源於網路 *******************//***************** *為Date對象添加定製的方法 ******************///擷取目前時間 Date.prototype.getTheDate=function(){return {Y:this.getFullYear(),M:this.getMonth()+1,D:this.getDate()};}Date.prototype.beforeTheDate=function(Y,M,D){if(Y<this.getFullYear()||(Y==this.getFullYear()&&M<this.getMonth()+1)||(Y==this.getFullYear()&&M==this.getMonth()+1&&D<this.getDate())){return true;}else{return false;}}Date.prototype.afterTheDate=function(Y,M,D){if(Y>this.getFullYear()||(Y==this.getFullYear()&&M>this.getMonth()+1)||(Y==this.getFullYear()&&M==this.getMonth()+1&&D>this.getDate())){return true;}else{return false;}}Date.prototype.getThePreDate=function(dateObj,minYear){var M=dateObj.M;var Y=dateObj.Y;var D=dateObj.D;M--;if(M<1){M=12;Y--;}if(Y<minYear){Y=minYear;}return "{Y:"+Y+",M:"+M+",D:"+D+"}";}Date.prototype.getTheNextDate=function(dateObj,maxYear){var M=dateObj.M;var Y=dateObj.Y;var D=dateObj.D;M++;if(M>12){M=1;Y++;}if(Y>maxYear){Y=maxYear;}return "{Y:"+Y+",M:"+M+",D:"+D+"}";}/***************** *日曆的建構函式 ******************/function MarcianCalendar(calendarObjName,display,minYear,maxYear,afterNotUsed,beforeNotUsed){/**********************************************參數說明*calendarObjName:建立的日曆對象執行個體名,必須*display:顯示日曆 true為顯示,false為不顯示.可選,預設為不顯示*minYear:最小年份,可選,預設為1900*maxYear:最打年份,可選,預設為2900*afterNotUsed:大於目前時間的日期不可用,可選,預設為false;*beforeNotUsed:小於當前日期的時間不可用,可選,預設為false;**********************************************//*屬性*/this.calendarName=calendarObjName;//建立的日曆對象執行個體名this.display=display?"block":"none";//this.minYear=minYear?minYear:1900;//this.maxYear=maxYear?maxYear:2900;//this.afterNotUsed=afterNotUsed?afterNotUsed:false;this.beforeNotUsed=beforeNotUsed?beforeNotUsed:false;this.calendarContainer=null;//日曆容器this.calendarMenuContainer=null;//日曆菜單容器this.calendarDateContainer=null;//日曆日期容器this.calendarWeekContainer=null;//日曆星期容器this.calendarCloseContainer=null;//關閉菜單容器this.weekAry=["日","一","二","三","四","五","六"];//星期數組this.date=new Date();//日期對象this.moveObjAry=new Array();//移動對象數組this.fillObjAry=new Array();//填充對象數組this.id=0;//當前ID號//擷取指定頁面元素的座標this.getObjOffset=function(obj){var x=obj.offsetWidth;var y=0;while(obj.offsetParent){x+=obj.offsetLeft;y+=obj.offsetTop;obj=obj.offsetParent;}return{x:x,y:y};}//移動日曆this.moveCalendar=function(id){var offset=this.getObjOffset(this.$(this.moveObjAry[id]));this.calendarContainer.style.cssText=this.calendarContainer.getAttribute("css")+";top:"+offset.y+"px;left:"+offset.x+"px;display:block";this.id=id;}//填充日期this.fillDate=function(dateStr){var fillObj=this.$(this.fillObjAry[this.id]);if(fillObj.type=="text"){fillObj.value=dateStr;}if(this.id<this.fillObjAry.length-1){this.id++;this.moveCalendar(this.id);}else{this.calendarContainer.style.cssText=this.calendarContainer.getAttribute("css")+"display:none;";}}//擷取頁面元素的捷徑this.$=function(id){return document.getElementById(id);}//顯示日曆的外部調用方法this.showCalendar=function(showIDAry,fillIDAry){ /*******************************參數說明*showIDAry:點擊顯示日曆的元素的ID數組*fillIDAry:要填充日期資料的元素的ID數組******************************/this.moveObjAry=showIDAry;this.fillObjAry=fillIDAry;this.moveCalendar(0);}/*設定日曆時間*/this.setDate=function(dateObj){if(this.calendarDateContainer.childNodes){for(var j=this.calendarDateContainer.childNodes.length-1;j>=0;j--){this.calendarDateContainer.removeChild(this.calendarDateContainer.childNodes[j]);}} var W=1;var Y=dateObj.Y;var M=dateObj.M;var D=dateObj.D;var dayAry=[31,28,31,30,31,30,31,31,30,31,30,31];var r=[0,3,3,6,1,4,6,2,5,0,3,5];var c=6;if(Y%400==0||(Y%4==0&&Y%100!=0)){dayAry[1]=29;}if((Y%400==0||(Y%4==0&&Y%100!=0))&&M<3){c=5;}var y=Y%400;w=(y+Math.floor(y/4)-Math.floor(y/100)+r[M-1]+1+c)%7;//alert(w);var R=1; R=(dayAry[M-1]-7+w)%7==0?R+(dayAry[M-1]-7+w)/7:R+Math.floor((dayAry[M-1]-7+w)/7)+1;var start=0;var d=1;for(var i=0;i<R;i++){var div=document.createElement("div");div.style.cssText="width:140px;height:23px;margin-bottom:2px;"for(var k=0;k<7;k++){var p=document.createElement("p");p.style.cssText="width:20px;height:20px;float:left;text-align:center;padding-top:3px;margin:0px;color:#ccc;";if(start>=w&&d<=dayAry[M-1]){if(this.beforeNotUsed&&this.date.beforeTheDate(Y,M,d)){p.innerHTML=d;}else if(this.afterNotUsed&&this.date.afterTheDate(Y,M,d)){p.innerHTML=d;}else{p.innerHTML=""+d+"";}d++;}start>w?start=w:start++;div.appendChild(p);} this.calendarDateContainer.appendChild(div);}}/*設定日曆控制功能表*/this.setMenu=function(dateObj){this.calendarMenuContainer.innerHTML="上月 "+dateObj.Y+"-"+(dateObj.M>9?dateObj.M:"0"+dateObj.M)+" 下月";this.setDate(dateObj);}/*初始化*/this.initial=function(){this.calendarContainer=document.createElement("div");this.calendarContainer.style.cssText="position:absolute;top:0px;left:0px;width:140px;border:1px solid #666;padding:1px;background:#fff;font:12px Arial;margin-bottom:1px;display:"+this.display+";";this.calendarContainer.setAttribute("css",this.calendarContainer.style.cssText);this.calendarMenuContainer=document.createElement("div");this.calendarMenuContainer.style.cssText="width:140px;height:20px;background:#aaa;color:#fff;text-align:center;padding-top:3px;";this.calendarWeekContainer=document.createElement("div");this.calendarWeekContainer.style.cssText="width:140px;height:20px;margin-bottom:1px;";for(i=0;i<7;i++){var p=document.createElement("p");p.style.cssText="width:20px;height:20px;float:left;text-align:center;padding-top:3px;border-bottom:1px solid #666;margin:0px;";p.innerHTML=this.weekAry[i];this.calendarWeekContainer.appendChild(p);}this.calendarDateContainer=document.createElement("div"); this.calendarDateContainer.style.cssText="width:140px;";this.calendarCloseContainer=document.createElement("div");this.calendarCloseContainer.style.cssText=this.calendarMenuContainer.style.cssText;this.calendarCloseContainer.innerHTML="關閉日曆";this.calendarContainer.appendChild(this.calendarMenuContainer);this.calendarContainer.appendChild(this.calendarWeekContainer); this.calendarContainer.appendChild(this.calendarDateContainer); this.calendarContainer.appendChild(this.calendarCloseContainer);document.body.appendChild(this.calendarContainer);this.setMenu(this.date.getTheDate());}//檢查輸入的年參數是否符合要求.this.checkYear=function(){if(!this.minYear.toString().match(/\d\d\d\d/)){alert("最低年份輸入不正確,重新輸入");}else if(!this.maxYear.toString().match(/\d\d\d\d/)){alert("最高年份輸入不正確,重新輸入");}else if(eval("0+"+this.maxYear)<=eval("0+"+this.minYear)){alert("最高年份必須大於最低年份,重新輸入");}else if(eval("0+"+this.maxYear)<this.date.getFullYear()){alert("最高年份必須大於或者等於當前年份");}else{ this.initial();}}this.checkYear();}//建立日曆calendar=new MarcianCalendar("calendar",false,"","",false,true); </script> <input type="text" id="text"/><input type="button" id="fill" value="日曆" onclick="calendar.showCalendar(['fill','hello','hello1'],['text','text1','text2'])"/><br/><br/><br/><br/> <input type="text" id="text1"><input type="button" id="hello" value="日曆" onclick="calendar.showCalendar(['hello','hello1'],['text1','text2'])"/> <br/><br/><br/><br/> <input type="text" id="text2"><input type="button" id="hello1" value="日曆" onclick="calendar.showCalendar(['hello1'],['text2'])"/> <br/><br/> </BODY></HTML>
提示:您可以先修改部分代碼再運行