<div class="codetitle"> <span><a style="CURSOR: pointer" data="30724" class="copybut" id="copybut30724" onclick="doCopy('code30724')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code30724"> <br><html> <br><head> <br><title>Js日期选择器并自动加入到输入框中</title> <br><meta http-equiv="content-Type" content="text/html;charset=gb2312"> <br><script type="text/javascript"> <br>var gMonths=new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"); <br>var WeekDay=new Array("日","一","二","三","四","五","六"); <br>var strToday="今天"; <br>var strYear="年"; <br>var strMonth="月"; <br>var strDay="日"; <br>var splitChar="-"; <br>var startYear=2000; <br>var endYear=2050; <br>var dayTdHeight=12; <br>var dayTdTextSize=12; <br>var gcNotCurMonth="#E0E0E0"; <br>var gcRestDay="#FF0000"; <br>var gcWorkDay="#444444"; <br>var gcMouseOver="#79D0FF"; <br>var gcMouseOut="#F4F4F4"; <br>var gcToday="#444444"; <br>var gcTodayMouseOver="#6699FF"; <br>var gcTodayMouseOut="#79D0FF"; <br>var gdCtrl=new Object(); <br>var goSelectTag=new Array(); <br>var gdCurDate=new Date(); <br>var giYear=gdCurDate.getFullYear(); <br>var giMonth=gdCurDate.getMonth() 1; <br>var giDay=gdCurDate.getDate(); <br>function $(){var elements=new Array();for(var i=0;i<arguments.length;i ) {var element=arguments[i];if(typeof(arguments[i])=='string'){element=document.getElementById(arguments[i]);}if(arguments.length==1){return element;}elements.Push(element);}return elements;} <BR>Array.prototype.Push=function(){var startLength=this.length;for(var i=0;i<arguments.length;i ){this[startLength i]=arguments[i];}return this.length;} <BR>String.prototype.HexToDec=function(){return parseInt(this,16);} <BR>String.prototype.cleanBlank=function(){return this.isEmpty()?"":this.replace(/s/g,"");} <BR>function checkColor(){var color_tmp=(arguments[0] "").replace(/s/g,"").toUpperCase();var model_tmp1=arguments[1].toUpperCase();var model_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){return true;}return false;} <BR>function $V(){return $(arguments[0]).value;} <BR>function fPopCalendar(evt,popCtrl,dateCtrl){evt.cancelBubble=true;gdCtrl=dateCtrl;fSetYearMon(giYear,giMonth);var point=fGetXY(popCtrl);with($("calendardiv").style){left=point.x "px";top=(point.y popCtrl.offsetHeight 1) "px";visibility='visible';zindex='99';position='absolute';}$("calendardiv").focus();} <BR>function fSetDate(iYear,iMonth,iDay){var iMonthNew=new String(iMonth);var iDayNew=new String(iDay);if(iMonthNew.length<2){iMonthNew="0" iMonthNew;}if(iDayNew.length<2){iDayNew="0" iDayNew;}gdCtrl.value=iYear splitChar iMonthNew splitChar iDayNew;fHideCalendar();} <BR>function fHideCalendar(){$("calendardiv").style.visibility="hidden";for(var i=0;i<goSelectTag.length;i ){goSelectTag[i].style.visibility="visible";}goSelectTag.length=0;} <BR>function fSetSelected(){var iOffset=0;var iYear=parseInt($("tbSelYear").value);var iMonth=parseInt($("tbSelMonth").value);var aCell=$("cellText" arguments[0]);aCell.bgColor=gcMouseOut;with(aCell){var iDay=parseInt(innerHTML);if(checkColor(style.color,gcNotCurMonth)){iOffset=(innerHTML>10)?-1:1;}iMonth =iOffset;if(iMonth<1){iYear--;iMonth=12;}else if(iMonth>12){iYear ;iMonth=1;}}fSetDate(iYear,iMonth,iDay);} <br>function Point(iX,iY){this.x=iX;this.y=iY;} <br>function fBuildCal(iYear,iMonth){var aMonth=new Array();for(var i=1;i<7;i ){aMonth[i]=new Array(i);}var dCalDate=new Date(iYear,iMonth-1,1);var iDayOfFirst=dCalDate.getDay();var iDaysInMonth=new Date(iYear,iMonth,0).getDate();var iOffsetLast=new Date(iYear,iMonth-1,0).getDate()-iDayOfFirst 1;var iDate=1;var iNext=1;for(var d=0;d<7;d ){aMonth[1][d]=(d<iDayOfFirst)?(iOffsetLast d)*(-1):iDate ;}for(var w=2;w<7;w ){for(var d=0;d<7;d ){aMonth[w][d]=(iDate<=iDaysInMonth)?iDate :(iNext )*(-1);}}return aMonth;} <BR>function fDrawCal(iYear,iMonth,iCellHeight,iDateTextSize){var colorTD=" bgcolor='" gcMouseOut "' bordercolor='" gcMouseOut "'";var styleTD=" valign='middle' align='center' style='height:" iCellHeight "px;font-weight:bolder;font-size:" iDateTextSize "px;";var dateCal="";dateCal ="<tr>";for(var i=0;i<7;i ){dateCal ="<td" colorTD styleTD "color:#990099'>" WeekDay[i] "</td>";}dateCal ="</tr>";for(var w=1;w<7;w ){dateCal ="<tr>";for(var d=0;d<7;d ){var tmpid=w "" d;dateCal ="<td" styleTD "cursor:pointer;' onclick='fSetSelected(" tmpid ")'>";dateCal ="<span id='cellText" tmpid "'></span>";dateCal ="</td>";}dateCal ="</tr>";}return dateCal;}<br>函数 fUpdateCal(iYear,iMonth){var myMonth=fBuildCal(iYear,iMonth);var i=0;for(var w=1;w函数 fSetYearMon(iYear,iMon){$("tbSelMonth").options[iMon-1].selected=true;for(var i=0;i函数 fPrevMonth(){var iMon=$("tbSelMonth").value;var iYear=$("tbSelYear").value;if(--iMonfunction fNextMonth(){var iMon=$("tbSelMonth").value;var iYear=$("tbSelYear").value;if( iMon>12 ){iMon=1;iYear ;}fSetYearMon(iYear,iMon);} <br>函数 fGetXY(aTag){var oTmp=aTag;var pt=new Point(0,0);do{pt.x =oTmp. offsetLeft;pt.y =oTmp.offsetTop;oTmp=oTmp.offsetParent;}while(oTmp.tagName.toUpperCase()!="BODY");return pt;} <br>function getDateDiv(){var noSelectForIE="" ;var noSelectForFireFox="";if(document.all){noSelectForIE="onselectstart='return false;'";}else{noSelectForFireFox="-moz-user-select:none;";}var dateDiv=""; dateDiv ="<div id='calendardiv' onclick='event.cancelBubble=true'" noSelectForIE " style='" noSelectForFireFox "position:absolute;z-index:99;visibility:hidden;border:1pxsolid #999999; '>";dateDiv ="<表格边框='0' bgcolor='#E0E0E0' cellpadding='1' cellspacing='1'>";dateDiv ="<tr>";dateDiv ="< td>";dateDiv ="</td><td><选择 id='tbSelYear' style='border:1pxsolid;' onchange='fUpdateCal($V("tbSelYear"),$V("tbSelMonth"))'>";for(var i=startYear;i<endYear;i ){dateDiv ="<选项值='"我“'>” i strYear "</option>";}dateDiv ="</select></td><td>";dateDiv ="<select id='tbSelMonth' style='border:1pxsolid;' onchange='fUpdateCal($V("tbSelYear"),$V("tbSelMonth"))'>";for(var i=0;i<12;i ){dateDiv ="<选项值='" (i 1) “'>” gMonths[i] "</option>";}dateDiv ="</select></td><td>";dateDiv ="<输入类型='按钮'id='下一个月'值= '>'样式='高度:20px;宽度:20px;字体粗细:粗体;' onclick='fNextMonth()'>";dateDiv ="</td>";dateDiv ="</tr><tr>";dateDiv ="<tdalign='center'colspan='4 '>";dateDiv ="<div style='background-color:#cccccc'><表格宽度='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="<span style='cursor:pointer;font-weight:bolder;' onclick='fSetDate(giYear,giMonth,giDay)' onmouseover='this.style.color="" gcMouseOver ""' onmouseout='this.style.color="#000000"'>" strToday ":" giYear strYear giMonth strMonth giDay strDay "</span>";dateDiv ="</tr></tr>";dateDiv ="</table></div>";返回 dateDiv ;} <br>with(document){onclick=fHideCalendar;write(getDateDiv());} <br>; <br></头> <br><身体> <br><输入类型=“文本”样式=“边框:1px实心#999;” onclick="fPopCalendar(event,this,this)" onfocus="this.select()" readonly="readonly" /> <BR></身体> <br></html> <br> </div>