第一步:将如下内容存为calendar.js(请替换《》为“<>”)
document.write("《div id=meizzcalendarlayer style=position: absolute; z-index: 9999; width: 144; height: 193; display: none》"); document.write("《iframe name=meizzcalendariframe scrolling=no frameborder=0 width=100% height=100%》《/iframe》《/div》"); function writeiframe() { var striframe = "《html》《head》《meta http-equiv=content-type content=text/html; charset=gb2312》《style》"+ "*{font-size: 12px; font-family: 宋体}"+ ".bg{ color: "+ webcalendar.lightcolor +"; cursor: default; background-color: "+ webcalendar.darkcolor +";}"+ "table#tablemain{ width: 142; height: 180;}"+ "table#tableweek td{ color: "+ webcalendar.lightcolor +";}"+ "table#tableday td{ font-weight: bold;}"+ "td#meizzyearhead, td#meizzyearmonth{color: "+ webcalendar.wordcolor +"}"+ ".out { text-align: center; border-top: 1px solid "+ webcalendar.darkborder +"; border-left: 1px solid "+ webcalendar.darkborder +";"+ "border-right: 1px solid "+ webcalendar.lightcolor +"; border-bottom: 1px solid "+ webcalendar.lightcolor +";}"+ ".over{ text-align: center; border-top: 1px solid #ffffff; border-left: 1px solid #ffffff;"+ "border-bottom: 1px solid "+ webcalendar.darkborder +"; border-right: 1px solid "+ webcalendar.darkborder +"}"+ "input{ border: 1px solid "+ webcalendar.darkcolor +"; padding-top: 1px; height: 18; cursor: hand;"+ " color:"+ webcalendar.wordcolor +"; background-color: "+ webcalendar.btnbgcolor +"}"+ "《/style》《/head》《body onselectstart=return false style=margin: 0px oncontextmenu=return false》《form name=meizz》"; if (webcalendar.drag){ striframe += "《scr"+"ipt language=javascript》"+ "var drag=false, cx=0, cy=0, o = parent.webcalendar.calendar; function document.onmousemove(){"+ "if(parent.webcalendar.drag && drag){if(o.style.left==)o.style.left=0; if(o.style.top==)o.style.top=0;"+ "o.style.left = parseint(o.style.left) + window.event.clientx-cx;"+ "o.style.top = parseint(o.style.top) + window.event.clienty-cy;}}"+ "function document.onkeydown(){ switch(window.event.keycode){ case 27 : parent.hiddencalendar(); break;"+ "case 37 : parent.prevm(); break; case 38 : parent.prevy(); break; case 39 : parent.nextm(); break; case 40 : parent.nexty(); break;"+ "case 84 : document.forms[0].today.click(); break;} window.event.keycode = 0; window.event.returnvalue= false;}"+ "function dragstart(){cx=window.event.clientx; cy=window.event.clienty; drag=true;}《/scr"+"ipt》"} striframe += "《select name=tmpyearselect onblur=parent.hiddenselect(this) style=z-index:1;position:absolute;top:3;left:18;display:none"+ " onchange=parent.webcalendar.thisyear =this.value; parent.hiddenselect(this); parent.writecalendar();》《/select》"+ "《select name=tmpmonthselect onblur=parent.hiddenselect(this) style=z-index:1; position:absolute;top:3;left:74;display:none"+ " onchange=parent.webcalendar.thismonth=this.value; parent.hiddenselect(this); parent.writecalendar();》《/select》"+ "《table id=tablemain class=bg border=0 cellspacing=2 cellpadding=0》"+ "《tr》《td width=140 height=19 bgcolor="+ webcalendar.lightcolor +"》"+ " 《table width=140 id=tablehead border=0 cellspacing=1 cellpadding=0》《tr align=center》"+ " 《td width=15 height=19 class=bg title=向前翻 1 月 快捷键:← style=cursor: hand onclick=parent.prevm()》《b》<《/b》《/td》"+ " 《td width=60 id=meizzyearhead title=点击此处选择年份 onclick=parent.funyearselect(parseint(this.innertext, 10))"+ " onmouseover=this.bgcolor=parent.webcalendar.darkcolor; this.style.color=parent.webcalendar.lightcolor"+ " onmouseout=this.bgcolor=parent.webcalendar.lightcolor; this.style.color=parent.webcalendar.wordcolor》《/td》"+ " 《td width=50 id=meizzyearmonth title=点击此处选择月份 onclick=parent.funmonthselect(parseint(this.innertext, 10))"+ " onmouseover=this.bgcolor=parent.webcalendar.darkcolor; this.style.color=parent.webcalendar.lightcolor"+ " onmouseout=this.bgcolor=parent.webcalendar.lightcolor; this.style.color=parent.webcalendar.wordcolor》《/td》"+ " 《td width=15 class=bg title=向后翻 1 月 快捷键:→ onclick=parent.nextm() style=cursor: hand》《b》>《/b》《/td》《/tr》《/table》"+ "《/td》《/tr》《tr》《td height=20》《table id=tableweek border=1 width=140 cellpadding=0 cellspacing=0 "; if(webcalendar.drag){striframe += "onmousedown=dragstart() onmouseup=drag=false onmouseout=drag=false";} striframe += " bordercolorlight="+ webcalendar.darkcolor +" bordercolordark="+ webcalendar.lightcolor +"》"+ " 《tr align=center》《td height=20》日《/td》《td》一《/td》《td》二《/td》《td》三《/td》《td》四《/td》《td》五《/td》《td》六《/td》《/tr》《/table》"+ "《/td》《/tr》《tr》《td valign=top width=140 bgcolor="+ webcalendar.lightcolor +"》"+ " 《table id=tableday height=120 width=140 border=0 cellspacing=1 cellpadding=0》"; for(var x=0; x《5; x++){ striframe += "《tr》"; for(var y=0; y《7; y++) striframe += "《td class=out id=meizzday"+ (x*7+y) +"》《/td》"; striframe += "《/tr》";} striframe += "《tr》"; for(var x=35; x《39; x++) striframe += "《td class=out id=meizzday"+ x +"》《/td》"; striframe +="《td colspan=3 class=out title="+ webcalendar.reginfo +"》《input style= background-color: "+ webcalendar.btnbgcolor +";cursor: hand; padding-top: 4px; width: 100%; height: 100%; border: 0 onfocus=this.blur()"+ " type=button value= 关闭 onclick=parent.hiddencalendar()》《/td》《/tr》《/table》"+ "《/td》《/tr》《tr》《td height=20 width=140 bgcolor="+ webcalendar.lightcolor +"》"+ " 《table border=0 cellpadding=1 cellspacing=0 width=140》"+ " 《tr》《td》《input name=prevyear title=向前翻 1 年 快捷键:↑ onclick=parent.prevy() type=button value=<<"+ " onfocus=this.blur() style=meizz:expression(this.disabled=parent.webcalendar.thisyear==1000)》《input"+ " onfocus=this.blur() name=prevmonth title=向前翻 1 月 快捷键:← onclick=parent.prevm() type=button value=< 》"+ " 《/td》《td align=center》《input name=today type=button value=今天 onfocus=this.blur() style=width: 50 title=当前日期 快捷键:t"+ " onclick=\"parent.returndate(new date().getdate() +/+ (new date().getmonth() +1) +/+ new date().getfullyear())\"》"+ " 《/td》《td align=right》《input title=向后翻 1 月 快捷键:→ name=nextmonth onclick=parent.nextm() type=button value= >"+ " onfocus=this.blur()》《input name=nextyear title=向后翻 1 年 快捷键:↓ onclick=parent.nexty() type=button value=>>"+ " onfocus=this.blur() style=meizz:expression(this.disabled=parent.webcalendar.thisyear==9999)》《/td》《/tr》《/table》"+ "《/td》《/tr》《table》《/form》《/body》《/html》"; with(webcalendar.iframe) { document.writeln(striframe); document.close(); for(var i=0; i《39; i++) { webcalendar.dayobj[i] = eval("meizzday"+ i); webcalendar.dayobj[i].onmouseover = daymouseover; webcalendar.dayobj[i].onmouseout = daymouseout; webcalendar.dayobj[i].onclick = returndate; } } } function twebcalendar() //初始化日历的设置 { this.reginfo = "点击关闭按钮关闭日期选择器!"; // this.reginfo = "web calendar 3.0 作者:meizz(梅花雪疏影横斜) 网站:http://www.meizz.com/ 关闭的快捷键:[esc]"; // this.reginfo += " ver 2.0:walkingpoison(水晶龙) ver 1.0:meizz(梅花雪疏影横斜)"; this.daysmonth = new array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); this.day = new array(39); //定义日历展示用的数组 this.dayobj = new array(39); //定义日期展示控件数组 this.datestyle = null; //保存格式化后日期数组 this.objexport = null; //日历回传的显示控件 this.eventsrc = null; //日历显示的触发控件 this.inputdate = null; //转化外的输入的日期(d/m/yyyy) this.thisyear = new date().getfullyear(); //定义年的变量的初始值 this.thismonth = new date().getmonth()+ 1; //定义月的变量的初始值 this.thisday = new date().getdate(); //定义日的变量的初始值 this.today = this.thisday +"/"+ this.thismonth +"/"+ this.thisyear; //今天(d/m/yyyy) this.iframe = window.frames("meizzcalendariframe"); //日历的 iframe 载体 this.calendar = getobjectbyid("meizzcalendarlayer"); //日历的层 this.datereg = ""; //日历格式验证的正则式 this.yearfall = 50; //定义年下拉框的年差值 this.format = "yyyy-mm-dd"; //回传日期的格式 this.timeshow = false; //是否返回时间 this.drag = true; //是否允许拖动 this.darkcolor = "#6bbfd9"; //控件的暗色 this.lightcolor = "#ffffff"; //控件的亮色 this.btnbgcolor = "#fffff5"; //控件的按钮背景色 this.wordcolor = "#000080"; //控件的文字颜色 this.worddark = "#dcdcdc"; //控件的暗文字颜色 this.daybgcolor = "#deeaf6"; //日期数字背景色 this.todaycolor = "#ff0000"; //今天在日历上的标示背景色 this.darkborder = "#d4d0c8"; //日期显示的立体表达色 } var webcalendar = new twebcalendar(); function calendar() //主调函数 { var e = window.event.srcelement; writeiframe(); var o = webcalendar.calendar.style; webcalendar.eventsrc = e; if (arguments.length == 0) webcalendar.objexport = e; else webcalendar.objexport = eval(arguments[0]); webcalendar.iframe.tableweek.style.cursor = webcalendar.drag ? "move" : "default"; var t = e.offsettop, h = e.clientheight, l = e.offsetleft, p = e.type; while (e = e.offsetparent){t += e.offsettop; l += e.offsetleft;} o.display = ""; webcalendar.iframe.document.body.focus(); var cw = webcalendar.calendar.clientwidth, ch = webcalendar.calendar.clientheight; var dw = document.body.clientwidth, dl = document.body.scrollleft, dt = document.body.scrolltop; if (document.body.clientheight + dt - t - h 》= ch) o.top = (p=="image")? t + h : t + h + 6; else o.top = (t - dt 《 ch) ? ((p=="image")? t + h : t + h + 6) : t - ch; if (dw + dl - l 》= cw) o.left = l; else o.left = (dw 》= cw) ? dw - cw + dl : dl; if (!webcalendar.timeshow) webcalendar.datereg = /^(\d{1,4})(-|\/|.)(\d{1,2})\2(\d{1,2})$/; else webcalendar.datereg = /^(\d{1,4})(-|\/|.)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; try{ if (webcalendar.objexport.value.trim() != ""){ webcalendar.datestyle = webcalendar.objexport.value.trim().match(webcalendar.datereg); if (webcalendar.datestyle == null) { webcalendar.thisyear = new date().getfullyear(); webcalendar.thismonth = new date().getmonth()+ 1; webcalendar.thisday = new date().getdate(); alert("原文本框里的日期有错误!\n可能与你定义的显示时分秒有冲突!"); writecalendar(); return false; } else { webcalendar.thisyear = parseint(webcalendar.datestyle[1], 10); webcalendar.thismonth = parseint(webcalendar.datestyle[3], 10); webcalendar.thisday = parseint(webcalendar.datestyle[4], 10); webcalendar.inputdate = parseint(webcalendar.thisday, 10) +"/"+ parseint(webcalendar.thismonth, 10) +"/"+ parseint(webcalendar.thisyear, 10); writecalendar(); } } else writecalendar(); } catch(e){writecalendar();} } function funmonthselect() //月份的下拉框 { var m = isnan(parseint(webcalendar.thismonth, 10)) ? new date().getmonth() + 1 : parseint(webcalendar.thismonth); var e = webcalendar.iframe.document.forms[0].tmpmonthselect; for (var i=1; i《13; i++) e.options.add(new option(i +"月", i)); e.style.display = ""; e.value = m; e.focus(); window.status = e.style.top; } function funyearselect() //年份的下拉框 { var n = webcalendar.yearfall; var e = webcalendar.iframe.document.forms[0].tmpyearselect; var y = isnan(parseint(webcalendar.thisyear, 10)) ? new date().getfullyear() : parseint(webcalendar.thisyear); y = (y 《= 1000)? 1000 : ((y 》= 9999)? 9999 : y); var min = (y - n 》= 1000) ? y - n : 1000; var max = (y + n 《= 9999) ? y + n : 9999; min = (max == 9999) ? max-n*2 : min; max = (min == 1000) ? min+n*2 : max; for (var i=min; i《=max; i++) { //alert(e.options.length); e.options[e.options.length] = new option(i +"年", i+"", true, true);//e.options.add(new option(i +"年", i)); } e.style.display = ""; e.value = y; e.focus(); } function prevm() //往前翻月份 { webcalendar.thisday = 1; if (webcalendar.thismonth==1) { webcalendar.thisyear--; webcalendar.thismonth=13; } webcalendar.thismonth--; writecalendar(); } function nextm() //往后翻月份 { webcalendar.thisday = 1; if (webcalendar.thismonth==12) { webcalendar.thisyear++; webcalendar.thismonth=0; } webcalendar.thismonth++; writecalendar(); } function prevy(){webcalendar.thisday = 1; webcalendar.thisyear--; writecalendar();}//往前翻 year function nexty(){webcalendar.thisday = 1; webcalendar.thisyear++; writecalendar();}//往后翻 year function hiddenselect(e){for(var i=e.options.length; i》-1; i--)e.options.remove(i); e.style.display="none";} function getobjectbyid(id){ if(document.all) return(eval("document.all."+ id)); return(eval(id)); } function hiddencalendar(){getobjectbyid("meizzcalendarlayer").style.display = "none";}; function appendzero(n){return(("00"+ n).substr(("00"+ n).length-2));}//日期自动补零程序 function string.prototype.trim(){return this.replace(/(^\s*)|(\s*$)/g,"");} function daymouseover() { this.classname = "over"; this.style.backgroundcolor = webcalendar.darkcolor; if(webcalendar.day[this.id.substr(8)].split("/")[1] == webcalendar.thismonth) this.style.color = webcalendar.lightcolor; } function daymouseout() { this.classname = "out"; var d = webcalendar.day[this.id.substr(8)], a = d.split("/"); this.style.removeattribute(backgroundcolor); if(a[1] == webcalendar.thismonth && d != webcalendar.today) { if(webcalendar.datestyle && a[0] == parseint(webcalendar.datestyle[4], 10)) this.style.color = webcalendar.lightcolor; this.style.color = webcalendar.wordcolor; } } function writecalendar() //对日历显示的数据的处理程序 { var y = webcalendar.thisyear; var m = webcalendar.thismonth; var d = webcalendar.thisday; webcalendar.daysmonth[1] = (0==y%4 && (y%100!=0 || y%400==0)) ? 29 : 28; if (!(y《=9999 && y 》= 1000 && parseint(m, 10)》0 && parseint(m, 10)《13 && parseint(d, 10)》0)){ alert("对不起,你输入了错误的日期!"); webcalendar.thisyear = new date().getfullyear(); webcalendar.thismonth = new date().getmonth()+ 1; webcalendar.thisday = new date().getdate(); } y = webcalendar.thisyear; m = webcalendar.thismonth; d = webcalendar.thisday; webcalendar.iframe.meizzyearhead.innertext = y +" 年"; webcalendar.iframe.meizzyearmonth.innertext = parseint(m, 10) +" 月"; webcalendar.daysmonth[1] = (0==y%4 && (y%100!=0 || y%400==0)) ? 29 : 28; //闰年二月为29天 var w = new date(y, m-1, 1).getday(); var prevdays = m==1 ? webcalendar.daysmonth[11] : webcalendar.daysmonth[m-2]; for(var i=(w-1); i》=0; i--) //这三个 for 循环为日历赋数据源(数组 webcalendar.day)格式是 d/m/yyyy { webcalendar.day[i] = prevdays +"/"+ (parseint(m, 10)-1) +"/"+ y; if(m==1) webcalendar.day[i] = prevdays +"/"+ 12 +"/"+ (parseint(y, 10)-1); prevdays--; } for(var i=1; i《=webcalendar.daysmonth[m-1]; i++) webcalendar.day[i+w-1] = i +"/"+ m +"/"+ y; for(var i=1; i《39-w-webcalendar.daysmonth[m-1]+1; i++) { webcalendar.day[webcalendar.daysmonth[m-1]+w-1+i] = i +"/"+ (parseint(m, 10)+1) +"/"+ y; if(m==12) webcalendar.day[webcalendar.daysmonth[m-1]+w-1+i] = i +"/"+ 1 +"/"+ (parseint(y, 10)+1); } for(var i=0; i《39; i++) //这个循环是根据源数组写到日历里显示 { var a = webcalendar.day[i].split("/"); webcalendar.dayobj[i].innertext = a[0]; webcalendar.dayobj[i].title = a[2] +"-"+ appendzero(a[1]) +"-"+ appendzero(a[0]); webcalendar.dayobj[i].bgcolor = webcalendar.daybgcolor; webcalendar.dayobj[i].style.color = webcalendar.wordcolor; if ((i《10 && parseint(webcalendar.day[i], 10)》20) || (i》27 && parseint(webcalendar.day[i], 10)《12)) webcalendar.dayobj[i].style.color = webcalendar.worddark; if (webcalendar.inputdate==webcalendar.day[i]) //设置输入框里的日期在日历上的颜色 {webcalendar.dayobj[i].bgcolor = webcalendar.darkcolor; webcalendar.dayobj[i].style.color = webcalendar.lightcolor;} if (webcalendar.day[i] == webcalendar.today) //设置今天在日历上反应出来的颜色 {webcalendar.dayobj[i].bgcolor = webcalendar.todaycolor; webcalendar.dayobj[i].style.color = webcalendar.lightcolor;} } } function returndate() //根据日期格式等返回用户选定的日期 { if(webcalendar.objexport) { var returnvalue; var a = (arguments.length==0) ? webcalendar.day[this.id.substr(8)].split("/") : arguments[0].split("/"); var d = webcalendar.format.match(/^(\w{4})(-|\/|.|)(\w{1,2})\2(\w{1,2})$/); if(d==null){alert("你设定的日期输出格式不对!\r\n\r\n请重新定义 webcalendar.format !"); return false;} var flag = d[3].length==2 || d[4].length==2; //判断返回的日期格式是否要补零 returnvalue = flag ? a[2] +d[2]+ appendzero(a[1]) +d[2]+ appendzero(a[0]) : a[2] +d[2]+ a[1] +d[2]+ a[0]; if(webcalendar.timeshow) { var h = new date().gethours(), m = new date().getminutes(), s = new date().getseconds(); returnvalue += flag ? " "+ appendzero(h) +":"+ appendzero(m) +":"+ appendzero(s) : " "+ h +":"+ m +":"+ s; } webcalendar.objexport.value = returnvalue; hiddencalendar(); } } function document.onclick() { if(webcalendar.eventsrc != window.event.srcelement) hiddencalendar(); }
第二步:新增一个页面testcalendar.htm,内容如下:(请替换《》为“<>”)
《!doctype html public "-//w3c//dtd html 4.01 transitional//en"》 《html》 《head》 《meta http-equiv="content-type" content="text/html; charset=gb2312"》 《title》日期控件《/title》 《style type="text/css"》.input1 { border-right: #d4d4d4 1px solid; border-top: #d4d4d4 1px solid; font-size: 12px; border-left: #d4d4d4 1px solid; border-bottom: #d4d4d4 1px solid; background-color: #ffffff } .input1 { border-right: #666666 1px solid; border-top: #666666 1px solid; font-size: 12px; border-left: #666666 1px solid; border-bottom: #666666 1px solid; background-color: #ffffff } .text1 { font-size: 12px; line-height: 18px; font-family: "arial", "helvetica", "sans-serif" } .style6 { font-size: 10px; line-height: 18px; font-family: "arial", "helvetica", "sans-serif" } .font00 { font-size: 12px } .font120 { font-size: 14px; line-height: 23px; font-family: "arial", "helvetica", "sans-serif" } .style7 { font-weight: bold; font-size: 12px; color: #3c8acd; font-family: "arial", "helvetica", "sans-serif" } .text11 { font-size: 14px; font-family: "arial", "helvetica", "sans-serif" } .style22 { color: #ff0000 } .text12 { font-size: 12px; font-family: "arial", "helvetica", "sans-serif" } .promptmsg { font-size: 12px; color: #ff0000; font-family: "arial", "helvetica", "sans-serif" } 《/style》 《script src="js/calendar.js" type="text/javascript"》《/script》 《/head》 《body》 《table》《tr》 《td class=style7 nowrap height=30》生 日: 《input class=input1 id=birthday onfocus=calendar() maxlength=10 name=birthday》 《font color=#ff0000》*《/font》 (yyyy-mm-dd) 《div class=promptmsg id=divbirthday》《/div》《/td》 《/tr》 《/table》 《/body》 《/html》
文章页数:[1]