1. 一个类似于Calendar日期控件,不用再刷新页面。
2. 判断文本控件里的值是否能转换成日期型。
.Js 例1:
/*
*coolWindowsCalendar.js
*
*/
//------------------------------------------------------------------------------------------------------
var bMoveable=true;
var _VersionInfo="原创:walkingpoison;修改者:不人不义;MAIL: [email protected] " ;
//==================================================== WEB 页面显示部分 =====================================================
var strFrame; //存放日历层的HTML代码
document.writeln('
1<iframe author="wayx" frameborder="0" id="meizzDateLayer" style="position: absolute; width: 144; height: 211; z-index: 9998; display: none"></iframe>
');
strFrame='
1<style>';
2strFrame+='INPUT.button{BORDER-RIGHT: #ff9900 1px solid;BORDER-TOP: #ff9900 1px solid;BORDER-LEFT: #ff9900 1px solid;';
3strFrame+='BORDER-BOTTOM: #ff9900 1px solid;BACKGROUND-COLOR: #fff8ec;font-family:宋体;}';
4strFrame+='TD{FONT-SIZE: 9pt;font-family:宋体;}';
5strFrame+='</style>
';
strFrame+='
1<scr' 'ipt="" +="">';
2strFrame+='var datelayerx,datelayery; /*存放日历控件的鼠标位置*/';
3strFrame+='var bDrag; /*标记是否开始拖动*/';
4strFrame+='function document.onmousemove() /*在鼠标移动事件中,如果开始拖动日历,则移动日历*/';
5strFrame+='{if(bDrag && window.event.button==1)';
6strFrame+=' {var DateLayer=parent.document.all.meizzDateLayer.style;';
7strFrame+=' DateLayer.posLeft += window.event.clientX-datelayerx;/*由于每次移动以后鼠标位置都恢复为初始的位置,因此写法与div中不同*/';
8strFrame+=' DateLayer.posTop += window.event.clientY-datelayery;}}';
9strFrame+='function DragStart() /*开始日历拖动*/';
10strFrame+='{var DateLayer=parent.document.all.meizzDateLayer.style;';
11strFrame+=' datelayerx=window.event.clientX;';
12strFrame+=' datelayery=window.event.clientY;';
13strFrame+=' bDrag=true;}';
14strFrame+='function DragEnd(){ /*结束日历拖动*/';
15strFrame+=' bDrag=false;}';
16strFrame+='</scr'>
';
strFrame+='
1<div onselectstart="return false" style="z-index:9999;position: absolute; left:0; top:0;"><span author="wayx" id="tmpSelectYearLayer" style="z-index: 9999;position: absolute;top: 3; left: 19;display: none"></span>';
2strFrame+='<span author="wayx" id="tmpSelectMonthLayer" style="z-index: 9999;position: absolute;top: 3; left: 78;display: none"></span>';
3strFrame+='<table author="wayx" bgcolor="#ff9900" border="1" bordercolor="#ff9900" cellpadding="0" cellspacing="0" height="160" width="142">';
4strFrame+=' <tr author="wayx"><td author="wayx" bgcolor="#FFFFFF" height="23" width="142"><table author="wayx" border="0" cellpadding="0" cellspacing="1" height="23" width="140">';
5strFrame+=' <tr align="center" author="wayx"><td #ffd700\'"="" '"="" ':'');="" ';="" 'move':'default')="" 'onmousedown="DragStart()" (bmoveable="" (bmoveable?="" +="" ;="" ?="" align="center" bgcolor="#ff9900" onclick="parent.tmpSelectMonthInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))" onmouseout="style.backgroundColor=\'white\'" onmouseup="DragEnd()" strframe+=' BORDERCOLORLIGHT=#FF9900 BORDERCOLORDARK=#FFFFFF width=140 height=20 Author="wayx" style="cursor:' style="font-size:12px;cursor: hand;color: #ffffff" white\'"="" width="16">';
6strFrame+='<tr align="center" author="wayx" valign="bottom"><td author="meizz" style="font-size:12px;color:#FFFFFF">日</td>';
7strFrame+='<td author="meizz" style="font-size:12px;color:#FFFFFF">一</td><td author="meizz" style="font-size:12px;color:#FFFFFF">二</td>';
8strFrame+='<td author="meizz" style="font-size:12px;color:#FFFFFF">三</td><td author="meizz" style="font-size:12px;color:#FFFFFF">四</td>';
9strFrame+='<td author="meizz" style="font-size:12px;color:#FFFFFF">五</td><td author="meizz" style="font-size:12px;color:#FFFFFF">六</td></tr>';
10strFrame+='</td></tr></table></td></tr><!-- Author:F.R.Huang(meizz) http://www.meizz.com/mail : [email protected] 2002-10-8 -->';
11strFrame+=' <tr author="wayx"><td author="wayx" height="120" width="142">';
12strFrame+=' <table author="wayx" bgcolor="#fff8ec" border="1" bordercolordark="#FFFFFF" bordercolorlight="#FF9900" cellpadding="0" cellspacing="2" height="120" width="140">';
13var n=0; for (j=0;j<5;j++){ strFrame+= ' <tr align="center" author="wayx">'; for (i=0;i<7;i++){
14strFrame+='<td author="meizz" height="20" id="meizzDay'+n+'" onclick="parent.meizzDayClick(this.innerText,0)" style="font-size:12px" width="20"></td>';n++;}
15strFrame+='</tr>';}
16strFrame+=' <tr align="center" author="wayx">';
17for (i=35;i<39;i++)strFrame+='<td author="wayx" height="20" id="meizzDay'+i+'" onclick="parent.meizzDayClick(this.innerText,0)" style="font-size:12px" width="20"></td>';
18strFrame+=' <td align="right" author="meizz" colspan="3"><span '"="" ';="" +="" _versioninfo="" onclick="parent.closeLayer()" strframe+=' Author=meizz title="' style="font-size:12px;cursor: hand"><u>关闭</u></span> </td></tr>';
19strFrame+=' </table></td></tr><tr author="wayx"><td author="wayx">';
20strFrame+=' <table author="wayx" bgcolor="#FFFFFF" border="0" cellpadding="0" cellspacing="1" width="100%">';
21strFrame+=' <tr author="wayx"><td align="left" author="meizz"><input ';="" (arguments.length="" ;="" =="WEB" author="meizz" class="button" function="" if="" odatelayer="window.frames.meizzDateLayer.document.all;" onclick="parent.meizzPrevY()" outbutton;="" outdate="" outobject;="" setday(tt,obj)="" strframe+="</tr></table></td></tr></table></div>" title="向前翻 1 年" type="button" value="<<" var="" window.frames.meizzdatelayer.document.close();="" window.frames.meizzdatelayer.document.writeln(strframe);="" {="" 主调函数="" 存放对象的日期="" 存放日历对象="" 点击的按钮="" 解决ie进度条不结束的问题="" 页面显示部分="var"/> 2){alert("对不起!传入本控件的参数太多!");return;}
22if (arguments.length == 0){alert("对不起!您没有传回本控件任何参数!");return;}
23var dads = document.all.meizzDateLayer.style;
24var th = tt;
25var ttop = tt.offsetTop; //TT控件的定位点高
26var thei = tt.clientHeight; //TT控件本身的高
27var tleft = tt.offsetLeft; //TT控件的定位点宽
28var ttyp = tt.type; //TT控件的类型
29while (tt = tt.offsetParent){ttop+=tt.offsetTop; tleft+=tt.offsetLeft;}
30dads.top = (ttyp=="image")? ttop+thei : ttop+thei+6;
31dads.left = tleft;
32outObject = (arguments.length == 1) ? th : obj;
33outButton = (arguments.length == 1) ? null : th; //设定外部点击的按钮
34//根据当前输入框的日期显示日历的年月
35var reg = /^(\d+)-(\d{1,2})-(\d{1,2})$/;
36var r = outObject.value.match(reg);
37if(r!=null){
38r[2]=r[2]-1;
39var d= new Date(r[1], r[2],r[3]);
40if(d.getFullYear()==r[1] && d.getMonth()==r[2] && d.getDate()==r[3]){
41outDate=d; //保存外部传入的日期
42}
43else outDate="";
44meizzSetDay(r[1],r[2]+1);
45}
46else{
47outDate="";
48meizzSetDay(new Date().getFullYear(), new Date().getMonth() + 1);
49}
50dads.display = '';
51
52event.returnValue=false;
53}
54
55var MonHead = new Array(12); //定义阳历中每个月的最大天数
56MonHead[0] = 31; MonHead[1] = 28; MonHead[2] = 31; MonHead[3] = 30; MonHead[4] = 31; MonHead[5] = 30;
57MonHead[6] = 31; MonHead[7] = 31; MonHead[8] = 30; MonHead[9] = 31; MonHead[10] = 30; MonHead[11] = 31;
58
59var meizzTheYear=new Date().getFullYear(); //定义年的变量的初始值
60var meizzTheMonth=new Date().getMonth()+1; //定义月的变量的初始值
61var meizzWDay=new Array(39); //定义写日期的数组
62
63function document.onclick() //任意点击时关闭该控件 //ie6的情况可以由下面的切换焦点处理代替
64{
65with(window.event)
66{ if (srcElement.getAttribute("Author")==null && srcElement != outObject && srcElement != outButton)
67closeLayer();
68}
69}
70
71function document.onkeyup() //按Esc键关闭,切换焦点关闭
72{
73if (window.event.keyCode==27){
74if(outObject)outObject.blur();
75closeLayer();
76}
77else if(document.activeElement)
78if(document.activeElement.getAttribute("Author")==null && document.activeElement != outObject && document.activeElement != outButton)
79{
80closeLayer();
81}
82}
83
84function meizzWriteHead(yy,mm) //往 head 中写入当前的年与月
85{
86odatelayer.meizzYearHead.innerText = yy + " 年";
87odatelayer.meizzMonthHead.innerText = mm + " 月";
88}
89
90function tmpSelectYearInnerHTML(strYear) //年份的下拉框
91{
92if (strYear.match(/\D/)!=null){alert("年份输入参数不是数字!");return;}
93var m = (strYear) ? strYear : new Date().getFullYear();
94if (m < 1000 || m > 9999) {alert("年份值不在 1000 到 9999 之间!");return;}
95var n = m - 10;
96if (n < 1000) n = 1000;
97if (n + 26 > 9999) n = 9974;
98var s = "<select "="" "'="" (i="m)" (var="" +="" 26;="" ;="" <="" author="meizz" for="" i="" i++)="" if="" n="" name="tmpSelectYear" none\"'="" none\";"="" s="" selected="" selectinnerhtml="s;" style="font-size: 12px" var="" {="" {selectinnerhtml="">" + i + "年" + "\r\n";}
99else {selectInnerHTML += "<option author="wayx" value='" + i + "'>" + i + "年" + "</option>\r\n";}
100}
101selectInnerHTML += "</select>";
102odatelayer.tmpSelectYearLayer.style.display="";
103odatelayer.tmpSelectYearLayer.innerHTML = selectInnerHTML;
104odatelayer.tmpSelectYear.focus();
105}
106
107function tmpSelectMonthInnerHTML(strMonth) //月份的下拉框
108{
109if (strMonth.match(/\D/)!=null){alert("月份输入参数不是数字!");return;}
110var m = (strMonth) ? strMonth : new Date().getMonth() + 1;
111var s = "<select "="" (i="m)" (var="" +="<option Author=wayx value='" +i+"'="" 13;="" ;="" <="" author="meizz" for="" i="" i++)="" if="" name="tmpSelectMonth" none\"'="" none\";"="" s="" selected="" selectinnerhtml="s;" style="font-size: 12px" var="" {="" {selectinnerhtml="">"+i+"月"+"\r\n";}
112else {selectInnerHTML += "<option author="wayx" value='"+i+"'>"+i+"月"+"</option>\r\n";}
113}
114selectInnerHTML += "</select>";
115odatelayer.tmpSelectMonthLayer.style.display="";
116odatelayer.tmpSelectMonthLayer.innerHTML = selectInnerHTML;
117odatelayer.tmpSelectMonth.focus();
118}
119
120function closeLayer() //这个层的关闭
121{
122document.all.meizzDateLayer.style.display="none";
123}
124
125function IsPinYear(year) //判断是否闰平年
126{
127if (0==year%4&&((year%100!=0)||(year%400==0))) return true;else return false;
128}
129
130function GetMonthCount(year,month) //闰年二月为29天
131{
132var c=MonHead[month-1];if((month==2)&&IsPinYear(year)) c++;return c;
133}
134function GetDOW(day,month,year) //求某天的星期几
135{
136var dt=new Date(year,month-1,day).getDay()/7; return dt;
137}
138
139function meizzPrevY() //往前翻 Year
140{
141if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear--;}
142else{alert("年份超出范围(1000-9999)!");}
143meizzSetDay(meizzTheYear,meizzTheMonth);
144}
145function meizzNextY() //往后翻 Year
146{
147if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear++;}
148else{alert("年份超出范围(1000-9999)!");}
149meizzSetDay(meizzTheYear,meizzTheMonth);
150}
151function meizzToday() //Today Button
152{
153var today;
154meizzTheYear = new Date().getFullYear();
155meizzTheMonth = new Date().getMonth()+1;
156today=new Date().getDate();
157//meizzSetDay(meizzTheYear,meizzTheMonth);
158if(outObject){
159outObject.value=meizzTheYear + "-" + meizzTheMonth + "-" + today;
160}
161closeLayer();
162}
163function meizzPrevM() //往前翻月份
164{
165if(meizzTheMonth>1){meizzTheMonth--}else{meizzTheYear--;meizzTheMonth=12;}
166meizzSetDay(meizzTheYear,meizzTheMonth);
167}
168function meizzNextM() //往后翻月份
169{
170if(meizzTheMonth==12){meizzTheYear++;meizzTheMonth=1}else{meizzTheMonth++}
171meizzSetDay(meizzTheYear,meizzTheMonth);
172}
173
174function meizzSetDay(yy,mm) //主要的写程序**********
175{
176meizzWriteHead(yy,mm);
177//设置当前年月的公共变量为传入值
178meizzTheYear=yy;
179meizzTheMonth=mm;
180
181for (var i = 0; i < 39; i++){meizzWDay[i]=""}; //将显示框的内容全部清空
182var day1 = 1,day2=1,firstday = new Date(yy,mm-1,1).getDay(); //某月第一天的星期几
183for (i=0;i<firstday;i++)meizzwday[i]=getmonthcount(mm==1?yy-1:yy,mm==1?12:mm-1)-firstday+i+1 "<="" (i="0;" (meizzwday[i]!="" )="" +="" 39;="" ;="" <="" da='eval("odatelayer.meizzDay"+i)' da.bordercolordark="#FFFFFF" da.bordercolorlight="#FF9900" da.innerhtml="<b><font color=gray>" day1="" font="" for="" getmonthcount(yy,mm)+1;="" i="" i++)="" i++){meizzwday[i]="day1;day1++;}" if="" if(i<firstday)="" meizzwday[i]="" var="" {="" 上个月的最后几天="" 上个月的部分="" 书写新的一个月的日期星期排列="" 初始化边框="">";
184da.title=(mm==1?12:mm-1) +"月" + meizzWDay[i] + "日";
185da.onclick=Function("meizzDayClick(this.innerText,-1)");
186if(!outDate)
187da.style.backgroundColor = ((mm==1?yy-1:yy) == new Date().getFullYear() &&
188(mm==1?12:mm-1) == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate()) ?
189"#FFD700":"#e0e0e0";
190else
191{
192da.style.backgroundColor =((mm==1?yy-1:yy)==outDate.getFullYear() && (mm==1?12:mm-1)== outDate.getMonth() + 1 &&
193meizzWDay[i]==outDate.getDate())? "#00ffff" :
194(((mm==1?yy-1:yy) == new Date().getFullYear() && (mm==1?12:mm-1) == new Date().getMonth()+1 &&
195meizzWDay[i] == new Date().getDate()) ? "#FFD700":"#e0e0e0");
196//将选中的日期显示为凹下去
197if((mm==1?yy-1:yy)==outDate.getFullYear() && (mm==1?12:mm-1)== outDate.getMonth() + 1 &&
198meizzWDay[i]==outDate.getDate())
199{
200da.borderColorLight="#FFFFFF";
201da.borderColorDark="#FF9900";
202}
203}
204}
205else if (i>=firstday+GetMonthCount(yy,mm)) //下个月的部分
206{
207da.innerHTML="<b><font color="gray">" + meizzWDay[i] + "</font></b>";
208da.title=(mm==12?1:mm+1) +"月" + meizzWDay[i] + "日";
209da.onclick=Function("meizzDayClick(this.innerText,1)");
210if(!outDate)
211da.style.backgroundColor = ((mm==12?yy+1:yy) == new Date().getFullYear() &&
212(mm==12?1:mm+1) == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate()) ?
213"#FFD700":"#e0e0e0";
214else
215{
216da.style.backgroundColor =((mm==12?yy+1:yy)==outDate.getFullYear() && (mm==12?1:mm+1)== outDate.getMonth() + 1 &&
217meizzWDay[i]==outDate.getDate())? "#00ffff" :
218(((mm==12?yy+1:yy) == new Date().getFullYear() && (mm==12?1:mm+1) == new Date().getMonth()+1 &&
219meizzWDay[i] == new Date().getDate()) ? "#FFD700":"#e0e0e0");
220//将选中的日期显示为凹下去
221if((mm==12?yy+1:yy)==outDate.getFullYear() && (mm==12?1:mm+1)== outDate.getMonth() + 1 &&
222meizzWDay[i]==outDate.getDate())
223{
224da.borderColorLight="#FFFFFF";
225da.borderColorDark="#FF9900";
226}
227}
228}
229else //本月的部分
230{
231da.innerHTML="<b>" + meizzWDay[i] + "</b>";
232da.title=mm +"月" + meizzWDay[i] + "日";
233da.onclick=Function("meizzDayClick(this.innerText,0)"); //给td赋予onclick事件的处理
234//如果是当前选择的日期,则显示亮蓝色的背景;如果是当前日期,则显示暗黄色背景
235if(!outDate)
236da.style.backgroundColor = (yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate())?
237"#FFD700":"#e0e0e0";
238else
239{
240da.style.backgroundColor =(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && meizzWDay[i]==outDate.getDate())?
241"#00ffff":((yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate())?
242"#FFD700":"#e0e0e0");
243//将选中的日期显示为凹下去
244if(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && meizzWDay[i]==outDate.getDate())
245{
246da.borderColorLight="#FFFFFF";
247da.borderColorDark="#FF9900";
248}
249}
250}
251da.style.cursor="hand"
252}
253else{da.innerHTML="";da.style.backgroundColor="";da.style.cursor="default"}
254}
255}
256
257function meizzDayClick(n,ex) //点击显示框选取日期,主输入函数*************
258{
259var yy=meizzTheYear;
260var mm = parseInt(meizzTheMonth)+ex; //ex表示偏移量,用于选择上个月份和下个月份的日期
261//判断月份,并进行对应的处理
262if(mm<1){
263yy--;
264mm=12+mm;
265}
266else if(mm>12){
267yy++;
268mm=mm-12;
269}
270
271if (mm < 10){mm = "0" + mm;}
272if (outObject)
273{
274if (!n) {//outObject.value="";
275return;}
276if ( n < 10){n = "0" + n;}
277outObject.value= yy + "-" + mm + "-" + n ; //注:在这里你可以输出改成你想要的格式
278outObject.realValue= yy + "-" + mm + "-" + n ;//与别一个JS文件接应 (xpMask.htc) // 囨囚囨図添加 //
279closeLayer();
280}
281else {closeLayer(); alert("您所要输出的控件对象并不存在!");}
282}
283//------------------------------------------------------------------------------------------------------
284
285Html 例:
286/*
287*<script src="../JScript/coolWindowsCalendar.js"></script>
288*setday(this,[object])和setday(this)
289*[object]是控件输出的控件名
290*<input name="txt"/>
291*<input onclick="setday(this,document.all.txt)" type="button" value="setday"/>
292*<input onfocus="setday(this)"/>
293*/
294
295如图: 
296
297接下遍 \---->></firstday;i++)meizzwday[i]=getmonthcount(mm==1?yy-1:yy,mm==1?12:mm-1)-firstday+i+1></td></tr></table></td></tr></table></div>