日历控件在FF2.0,ie6下测试可用

时间:2021-05-18

/*********************************************************
*使用方法
*控件调用onclick="fPopCalendar(event,this,this)"
*如下,给控件设置为readonly
*<inputtype="text"style="border:1pxsolid#cccccc;"
*size="15"onclick="fPopCalendar(event,this,this)"
*onfocus="this.select()"readonly="readonly"/>
*
*如果页面乱码,把下面包含汉字的定义项放到页面中即可
********************************************************/
vargMonths=newArray("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
varWeekDay=newArray("日","一","二","三","四","五","六");
varstrToday="今天";
varstrYear="年";
varstrMonth="月";
varstrDay="日";
varsplitChar="-";
varstartYear=2000;
varendYear=2050;
vardayTdHeight=12;
vardayTdTextSize=12;
vargcNotCurMonth="#E0E0E0";
vargcRestDay="#FF0000";
vargcWorkDay="#444444";
vargcWeekDay="#990099";
vargcMouseOver="#79D0FF";
vargcMouseOut="#F4F4F4";
vargcToday="#444444";
vargcTodayMouseOver="#6699FF";
vargcTodayMouseOut="#CCCCFF";
vargdCtrl=newObject();
vargoSelectTag=newArray();
vargdCurDate=newDate();
vargiYear=curYear=gdCurDate.getFullYear();
vargiMonth=curMonth=gdCurDate.getMonth()+1;
vargiDay=curDay=gdCurDate.getDate();
function$(){varelements=newArray();for(vari=0;i<arguments.length;i++){varelement=arguments[i];if(typeof(arguments[i])=='string'){element=document.getElementById(arguments[i]);}if(arguments.length==1){returnelement;}elements.Push(element);}returnelements;}
Array.prototype.Push=function(){varstartLength=this.length;for(vari=0;i<arguments.length;i++){this[startLength+i]=arguments[i];}returnthis.length;}
String.prototype.HexToDec=function(){returnparseInt(this,16);}
String.prototype.cleanBlank=function(){returnthis.isEmpty()?"":this.replace(/\s/g,"");}
functioncheckColor(){varcolor_tmp=(arguments[0]+"").replace(/\s/g,"").toUpperCase();varmodel_tmp1=arguments[1].toUpperCase();varmodel_tmp2="rgb("+arguments[1].substring(1,3).HexToDec()+","+arguments[1].substring(1,3).HexToDec()+","+arguments[1].substring(5).HexToDec()+")";model_tmp2=model_tmp2.toUpperCase();if(color_tmp==model_tmp1||color_tmp==model_tmp2){returntrue;}returnfalse;}
function$V(){return$(arguments[0]).value;}
functionfPopCalendar(evt,popCtrl,dateCtrl){evt.cancelBubble=true;gdCtrl=dateCtrl;varevtDate="";if(document.all){evtDate=evt.srcElement.value;}else{evtDate=evt.currentTarget.value;}varreg=/(^\d{4})\-([0-1]\d)\-([0-2]\d$)/;if(reg.test(evtDate)){reg.exec();giYear=RegExp.$1;giMonth=RegExp.$2;giDay=RegExp.$3;}fSetYearMon(giYear,giMonth);varpoint=fGetXY(popCtrl);with($("calendardiv").style){left=point.x+"px";top=(point.y+popCtrl.offsetHeight+1)+"px";visibility='visible';zindex='99';position='absolute';}$("calendardiv").focus();}
functionfSetDate(iYear,iMonth,iDay){variMonthNew=newString(iMonth);variDayNew=newString(iDay);if(iMonthNew.length<2){iMonthNew="0"+iMonthNew;}if(iDayNew.length<2){iDayNew="0"+iDayNew;}gdCtrl.value=iYear+splitChar+iMonthNew+splitChar+iDayNew;fHideCalendar();}
functionfHideCalendar(){$("calendardiv").style.visibility="hidden";for(vari=0;i<goSelectTag.length;i++){goSelectTag[i].style.visibility="visible";}goSelectTag.length=0;}
functionfSetSelected(){variOffset=0;variYear=parseInt($("tbSelYear").value);variMonth=parseInt($("tbSelMonth").value);varaCell=$("cellText"+arguments[0]);with(aCell){variDay=parseInt(innerHTML);if(checkColor(style.color,gcNotCurMonth)){iOffset=(innerHTML>10)?-1:1;}iMonth+=iOffset;if(iMonth<1){iYear--;iMonth=12;}elseif(iMonth>12){iYear++;iMonth=1;}}fSetDate(iYear,iMonth,iDay);}
functionPoint(iX,iY){this.x=iX;this.y=iY;}
functionfBuildCal(iYear,iMonth){varaMonth=newArray();for(vari=1;i<7;i++){aMonth[i]=newArray(i);}vardCalDate=newDate(iYear,iMonth-1,1);variDayOfFirst=dCalDate.getDay();variDaysInMonth=newDate(iYear,iMonth,0).getDate();variOffsetLast=newDate(iYear,iMonth-1,0).getDate()-iDayOfFirst+1;variDate=1;variNext=1;for(vard=0;d<7;d++){aMonth[1][d]=(d<iDayOfFirst)?(iOffsetLast+d)*(-1):iDate++;}for(varw=2;w<7;w++){for(vard=0;d<7;d++){aMonth[w][d]=(iDate<=iDaysInMonth)?iDate++:(iNext++)*(-1);}}returnaMonth;}
functionfDrawCal(iYear,iMonth,iCellHeight,iDateTextSize){varcolorTD="background-color:"+gcMouseOut+";border-color:"+gcMouseOut+";";varstyleTD="height:"+iCellHeight+"px;font-weight:bolder;font-size:"+iDateTextSize+"px;vertical-align:middle;text-align:center;";vardateCal="";dateCal+="<tr>";for(vari=0;i<7;i++){dateCal+="<tdstyle='"+colorTD+styleTD+"color:"+gcWeekDay+";'>"+WeekDay[i]+"</td>";}dateCal+="</tr>";for(varw=1;w<7;w++){dateCal+="<tr>";for(vard=0;d<7;d++){vartmpid=w+""+d;dateCal+="<tdstyle='"+styleTD+"cursor:pointer;'onclick='fSetSelected("+tmpid+")'>";dateCal+="<spanid='cellText"+tmpid+"'></span>";dateCal+="</td>";}dateCal+="</tr>";}returndateCal;}
functionfUpdateCal(iYear,iMonth){varmyMonth=fBuildCal(iYear,iMonth);vari=0;for(varw=1;w<7;w++){for(vard=0;d<7;d++){with($("cellText"+w+""+d)){parentNode.style.backgroundColor=gcMouseOut;parentNode.style.borderColor=gcMouseOut;parentNode.onmouseover=function(){this.style.backgroundColor=gcMouseOver;};parentNode.onmouseout=function(){this.style.backgroundColor=gcMouseOut;};if(myMonth[w][d]<0){style.color=gcNotCurMonth;innerHTML=Math.abs(myMonth[w][d]);}else{style.color=((d==0)||(d==6))?gcRestDay:gcWorkDay;innerHTML=myMonth[w][d];if(iYear==giYear&&iMonth==giMonth&&myMonth[w][d]==giDay){parentNode.style.backgroundColor=gcMouseOver;parentNode.onmouseover=function(){this.style.backgroundColor=gcMouseOver;};parentNode.onmouseout=function(){this.style.backgroundColor=gcMouseOver;};}if(iYear==curYear&&iMonth==curMonth&&myMonth[w][d]==curDay){style.color=gcToday;parentNode.style.backgroundColor=gcTodayMouseOut;parentNode.onmouseover=function(){this.style.backgroundColor=gcTodayMouseOver;};parentNode.onmouseout=function(){this.style.backgroundColor=gcTodayMouseOut;};}}}}}}
functionfCurrentDate(){returncurYear+strYear+curMonth+strMonth+curDay+strDay;}
functionfSetYearMon(iYear,iMon){$("tbSelMonth").options[iMon-1].selected=true;for(vari=0;i<$("tbSelYear").length;i++){if($("tbSelYear").options[i].value==iYear){$("tbSelYear").options[i].selected=true;}}fUpdateCal(iYear,iMon);}
functionfPrevMonth(){variMon=$("tbSelMonth").value;variYear=$("tbSelYear").value;if(--iMon<1){iMon=12;iYear--;}fSetYearMon(iYear,iMon);}
functionfNextMonth(){variMon=$("tbSelMonth").value;variYear=$("tbSelYear").value;if(++iMon>12){iMon=1;iYear++;}fSetYearMon(iYear,iMon);}
functionfGetXY(aTag){varoTmp=aTag;varpt=newPoint(0,0);do{pt.x+=oTmp.offsetLeft;pt.y+=oTmp.offsetTop;oTmp=oTmp.offsetParent;}while(oTmp.tagName.toUpperCase()!="BODY");returnpt;}
functiongetDateDiv(){varnoSelectForIE="";varnoSelectForFireFox="";if(document.all){noSelectForIE="onselectstart='returnfalse;'";}else{noSelectForFireFox="-moz-user-select:none;";}vardateDiv="";dateDiv+="<divid='calendardiv'onclick='event.cancelBubble=true'"+noSelectForIE+"style='"+noSelectForFireFox+"position:absolute;z-index:99;visibility:hidden;border:1pxsolid#999999;'>";dateDiv+="<tablestyle='border:0px;background-color:#E0E0E0;'cellpadding='1'cellspacing='1'>";dateDiv+="<tr>";dateDiv+="<td><inputtype='button'id='PrevMonth'value='&lt;'style='height:20px;width:20px;font-weight:bolder;'onclick='fPrevMonth()'>";dateDiv+="</td><td><selectid='tbSelYear'style='border:1pxsolid;'onchange='fUpdateCal($V(\"tbSelYear\"),$V(\"tbSelMonth\"))'>";for(vari=startYear;i<endYear;i++){dateDiv+="<optionvalue='"+i+"'>"+i+strYear+"</option>";}dateDiv+="</select></td><td>";dateDiv+="<selectid='tbSelMonth'style='border:1pxsolid;'onchange='fUpdateCal($V(\"tbSelYear\"),$V(\"tbSelMonth\"))'>";for(vari=0;i<12;i++){dateDiv+="<optionvalue='"+(i+1)+"'>"+gMonths[i]+"</option>";}dateDiv+="</select></td><td>";dateDiv+="<inputtype='button'id='NextMonth'value='&gt;'style='height:20px;width:20px;font-weight:bolder;'onclick='fNextMonth()'>";dateDiv+="</td>";dateDiv+="</tr><tr>";dateDiv+="<tdalign='center'colspan='4'>";dateDiv+="<divstyle='background-color:#cccccc'><tablewidth='100%'border='0'cellpadding='3'cellspacing='1'>";dateDiv+=fDrawCal(giYear,giMonth,dayTdHeight,dayTdTextSize);dateDiv+="</table></div>";dateDiv+="</td>";dateDiv+="</tr><tr><tdalign='center'colspan='4'nowrap>";dateDiv+="<spanstyle='cursor:pointer;font-weight:bolder;'onclick='fSetDate(curYear,curMonth,curDay)'onmouseover='this.style.color=\""+gcMouseOver+"\"'onmouseout='this.style.color=\"#000000\"'>"+strToday+":"+fCurrentDate()+"</span>";dateDiv+="</tr></tr>";dateDiv+="</table></div>";returndateDiv;}
with(document){onclick=fHideCalendar;write(getDateDiv());} [Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
日历控件.htm
复制代码 代码如下:
<?xmlversion="1.0"encoding="gb2312"?>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="zh-ch"lang="zh-ch">
<head>
<title>日历控件_</title>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
<metahttp-equiv="imagetoolbar"content="no"/>
<metahttp-equiv="pragma"content="no-cache"/>
<metahttp-equiv="Cache-Control"content="no-cache,must-revalidate"/>
<metahttp-equiv="expires"content="0"/>
<scriptsrc="date.js"type="text/javascript"></script>
</head>
<body>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<inputtype="text"style="border:1pxsolid#cccccc;"size="15"onclick="fPopCalendar(event,this,this)"onfocus="this.select()"readonly="readonly"id="date_input"/>
<inputtype="button"value="清除日期"onclick="$('date_input').value=''"/>
</body>
</html>

date.js
复制代码 代码如下:

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章