//调用方法:onclick="return calendar('输入框id');" var monthdnum=new array(0,31,28,31,30,31,30,31,31,30,31,30,31); var monthtext=new array("","一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"); var calendar_obj,calendar_obj2; //var writehead=1; document.write('') /*头部信息css及js函数*/ var headmsg=''+ ''+ 'var mdown,sy,ey,sm,em,hideselect=1,currentselect,year,month,day;function scroll(str,key)'+ '{var html="",s,e,obj,d;'+ 'if(key=="y"){s=sy;e=ey;obj="selectyearitem";d="年";}if(key=="m"){s=sm;e=em;obj="selectmonthitem";d="";}'+ 'if(str=="up"){s--;e--;}if(str=="next"){s++;e++;}'+ 'if(key=="y"){sy=s;ey=e;}if(key=="m"){if(s<1)s=1;if(s>6)s=6;if(e<7)e=7;if(e>12)e=12;sm=s;em=e;}'+ 'for(i=s;i<=e;i++){html+=\'\'+parent.monthtext[i]:i+\',\'+month+\',\'+day+\');">\'+i;html+=d+"";}'+ 'document.getelementbyid(obj).innerhtml=html;}'+ 'function keydown(str){if(str==38){scroll("up",currentselect);}if(str==40){scroll("next",currentselect);}}'+ 'function showselect(str,str2){hideselect=0;hselect();scroll(\'\',str2);document.getelementbyid(str).style.display="block";currentselect=str2}'+ 'function hselect(){document.getelementbyid("selectyear").style.display="none";document.getelementbyid("selectmonth").style.display="none";}'+ 'function calendar_getdate(y,m,d)'+ '{var objevent=window.event||arguments.callee.caller.arguments[0];'+ 'var srcelement=objevent.srcelement;'+ 'if(!srcelement){srcelement=objevent.target;}'+ 'if(srcelement.tagname=="a"){parent.calendar_getdate(y,m,srcelement.innerhtml);}}'+ 'document.onclick=function(){if(hideselect){hselect();}hideselect=1;}'+ '
5
'+ '
6
'+ '
5
'+ '
6
'; function calendar_show(year,month,day) { var content; var date=new date(); var daynum=(isleapyear(year) && month==2)?monthdnum[month]+1:monthdnum[month]; var date2=new date(year,month-1,1) var start=date2.getday(); var date2=null; var trn=math.ceil((daynum+start)/7); var prnum=1; content='year='+year+',month='+month+',day='+day+',sy=year-3;ey=year+3;'+ 'sm=(month-3<1)?1:(+month-3>6)?6:month-3;'+ 'em=(month+3<7)?7:(month+3>12)?12:month+3;
'+ '
'+ '
'+ '
'; //层定位 var calendar=document.getelementbyid('divcalendar'); calendar.style.height=66+21*trn+'px'; //框架高度 divcalendar.document.open() divcalendar.document.write(headmsg+content); divcalendar.document.close() var p=calendar_obj; var et=0,el=0,eh=0,dh=0,st=0,ep=p while(p && p.tagname!="body"){ et+=p.offsettop; //距窗口顶部距离 el+=p.offsetleft; //距窗口左边距离 p=p.offsetparent;} var eh=ep.offsetheight; //输入框高度 var ew=ep.offsetwidth; //输入框宽度 var dh=calendar.style.pixelheight; //框架高度 calendar.style.top=(et+dh>document.body.scrollheight && et-dh>=0)?et-dh+'px':et+eh+'px'; calendar.style.left=(el+206>document.body.scrollwidth && el-206+ew>=0)?el-206+ew+'px':el+'px'; settimeout("document.getelementbyid('divcalendar').style.display=''",1); } /*是否润年*/ function isleapyear(y){ if(0==y%4 && ((y%100!=0)||(y%400==0))){return true;} else{return false;} } function calendar_modif(y,m,d) { if(m<1){m=12;y=y-1;} if(m>12){m=1;y=y+1;} var num=(isleapyear(y) && m==2)?monthdnum[m]+1:monthdnum[m]; if(d>num){d=num;} calendar_show(y,m,d) } function calendar(obj,obj2) { var p=calendar_obj=document.getelementbyid(obj); calendar_obj2=document.getelementbyid(obj2); var re=/\d{4}-\d{1,2}-\d{1,2}/; var arr,sdate; arr=re.exec(calendar_obj.value); if(arr!=null){sdate=calendar_obj.value;} else{var mydate=new date(); sdate=mydate.getfullyear()+'-'+(mydate.getmonth()+1)+'-'+mydate.getdate();} sdate=sdate.split("-"); calendar_show(parseint(sdate[0]),parseint(sdate[1]),parseint(sdate[2])); } function calendar_getdate(y,m,d) { if(!calendar_obj2||d!=0){ if(d>0){calendar_obj.value=y+'-'+m+'-'+d;} else{ var objevent=divcalendar.event||arguments.callee.caller.arguments[0]; var srcelement=objevent.srcelement; if(!srcelement){srcelement=objevent.target;} calendar_obj.value=y+'-'+m+'-'+srcelement.innerhtml; } } else{ if(isleapyear(y) && m==2){num=monthdnum[m]+1;} else{num=monthdnum[m];} calendar_obj.value=y+'-'+m+'-1'; calendar_obj2.value=y+'-'+m+'-'+num; } calendar_hide();} function calendar_hide() {document.getelementbyid("divcalendar").style.display="none";} document.onclick=calendar_hide;