最终日历控件实例代码地址:http://www.oschina.net/code/snippet_2352644_55198
一.加入上一月下一月
我们先修改结构,加入上一月和下一月的按钮:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>date</title> <style type="text/css"> #box{width:350px;} #title{width:350px; height:50px;} #month{ float:left;width:60px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #year{float:left;width:80px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #week{ width:350px;height:50px;} #week div{ float:left; width:48px; height:48px; margin:1px; background:#C90; color:#fff; text-align:center; line-height:48px; cursor:pointer;} #con{ width:350px;} #con div{ float:left; width:48px; height:48px; margin:1px; background:#999; color:#333; text-align:center; line-height:48px; cursor:pointer;} #con div.now{background:#09F; color:#fff;} #prevmonth,#nextmonth{float:left;width:50px; height:50px;text-align:center;cursor:pointer;line-height:50px;} </style> </head> <body> <div id="box"> <div id="title"> <div id="prevmonth"><</div> <div id="month"></div> <div id="year"></div> <div id="nextmonth">></div> </div> <div id="week"> <div>日</div> <div>一</div> <div>二</div> <div>三</div> <div>四</div> <div>五</div> <div>六</div> </div> <div id="con"></div> </div> </body> <script type="text/javascript"> window.onload=function(){ //===================get ele=============================== var omonth=document.getElementById("month"); var oyear=document.getElementById("year"); var con=document.getElementById("con"); //===================set year month=============================== //默认时间对象 var dateObj = new Date(); //动态控制 //dateObj.setMonth(1); //年月获取 var year=dateObj.getFullYear(); var month=toyear(dateObj);//0是12月 //月年的显示 omonth.innerHTML=month+"月"; oyear.innerHTML=year+"年"; //===================set day=============================== //获取本月1号的周值 var oneweek=oneyearoneday(dateObj); //本月总日数 var alld=alldays(dateObj); //当前是几 var nowd=nowday(dateObj); //初始化显示本月信息 init(oneweek,alld,nowd); //===================function=============================== //初始化日期显示方法 function init(oneweek,alld,nowd){ for(var i=1;i<=oneweek;i++){//留空 var eday=document.createElement("div"); eday.innerHTML=""; con.appendChild(eday); }; for(var i=1;i<=alld;i++){//正常区域 var eday=document.createElement("div"); if(i==nowd){ eday.innerHTML=i; eday.className="now"; con.appendChild(eday); }else{ eday.innerHTML=i; con.appendChild(eday); }; }; }; //获取本月1号的周值 function oneyearoneday(dateObj){ var oneyear = new Date(); var year=dateObj.getFullYear(); var month=dateObj.getMonth();//0是12月 oneyear.setFullYear(year); oneyear.setMonth(month);//0是12月 oneyear.setDate(1); return oneyear.getDay(); }; //当前是几 function nowday(dateObj){ return dateObj.getDate(); }; //获取本月总日数方法 function alldays(dateObj){ var year=dateObj.getFullYear(); var month=dateObj.getMonth(); if(isLeapYear(year)){//闰年 switch(month) { case 0: return "31"; break; case 1: return "29"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }else{//平年 switch(month) { case 0: return "31"; break; case 1: return "28"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }; }; //闰年判断函数 function isLeapYear(year){ if( (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)){ return true; }else{ return false; }; }; //月份转化方法 function toyear(dateObj){ var month=dateObj.getMonth() switch(month) { case 0: return "1"; break; case 1: return "2"; break; case 2: return "3"; break; case 3: return "4"; break; case 4: return "5"; break; case 5: return "6"; break; case 6: return "7"; break; case 7: return "8"; break; case 8: return "9"; break; case 9: return "10"; break; case 10: return "11"; break; case 11: return "12"; break; default: }; }; }; </script> </html>
效果截图:
现在我们就要进一步处理,就是点击上一个月或者一下月修改日的显示,并且上面的年月也要修改,不断上一月我们会到2015年甚至更以前。
二.开发上一月下一月控制程序
拿到:
var prevmonth=document.getElementById("prevmonth"); var nextmonth=document.getElementById("nextmonth");
事件就是单击事件了,我们简单处理:
prevmonth.onclick=function(){//上一月 }; nextmonth.onclick=function(){//下一月 };
贪多嚼不烂,我们先把上一月处理好,再去看下一月:
上一月,就和上篇博客最后一样,我们只要把月设置为当前-1;
//动态控制 prevmonth.onclick=function(){//上一月 var ddm=dateObj.getMonth()-1; dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); };
当然,二次赋值前先清除:
function remove(){ con.innerHTML=""; };
我们还要加个判断限制,当处在1月,在点击上一月,年要发生变化,月变为上一年的12月:
prevmonth.onclick=function(){//上一月 var ddm=null; var ddy=null; if((dateObj.getMonth()-1)==-1){ ddm=11; ddy=dateObj.getFullYear()-1; }else{ ddm=dateObj.getMonth()-1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); };
看现在全部代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>date</title> <style type="text/css"> #box{width:350px;} #title{width:350px; height:50px;} #month{ float:left;width:60px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #year{float:left;width:80px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #week{ width:350px;height:50px;} #week div{ float:left; width:48px; height:48px; margin:1px; background:#C90; color:#fff; text-align:center; line-height:48px; cursor:pointer;} #con{ width:350px;} #con div{ float:left; width:48px; height:48px; margin:1px; background:#999; color:#333; text-align:center; line-height:48px; cursor:pointer;} #con div.now{background:#09F; color:#fff;} #prevmonth,#nextmonth{float:left;width:50px; height:50px;text-align:center;cursor:pointer;line-height:50px;} </style> </head> <body> <div id="box"> <div id="title"> <div id="prevmonth"><</div> <div id="month"></div> <div id="year"></div> <div id="nextmonth">></div> </div> <div id="week"> <div>日</div> <div>一</div> <div>二</div> <div>三</div> <div>四</div> <div>五</div> <div>六</div> </div> <div id="con"></div> </div> </body> <script type="text/javascript"> window.onload=function(){ //===================get ele=============================== var omonth=document.getElementById("month"); var oyear=document.getElementById("year"); var con=document.getElementById("con"); var prevmonth=document.getElementById("prevmonth"); var nextmonth=document.getElementById("nextmonth"); //===================set year month=============================== //默认时间对象 var dateObj = new Date(); //动态控制 prevmonth.onclick=function(){//上一月 var ddm=null; var ddy=null; if((dateObj.getMonth()-1)==-1){ ddm=11; ddy=dateObj.getFullYear()-1; }else{ ddm=dateObj.getMonth()-1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextmonth.onclick=function(){//下一月 }; //年月获取 var year=dateObj.getFullYear(); var month=toyear(dateObj);//0是12月 //月年的显示 omonth.innerHTML=month+"月"; oyear.innerHTML=year+"年"; //===================set day=============================== //获取本月1号的周值 var oneweek=oneyearoneday(dateObj); //本月总日数 var alld=alldays(dateObj); //当前是几 var nowd=nowday(dateObj); //初始化显示本月信息 init(oneweek,alld,nowd); //===================function=============================== //初始化日期显示方法 function remove(){ con.innerHTML=""; }; function init(oneweek,alld,nowd){ for(var i=1;i<=oneweek;i++){//留空 var eday=document.createElement("div"); eday.innerHTML=""; con.appendChild(eday); }; for(var i=1;i<=alld;i++){//正常区域 var eday=document.createElement("div"); if(i==nowd){ eday.innerHTML=i; eday.className="now"; con.appendChild(eday); }else{ eday.innerHTML=i; con.appendChild(eday); }; }; }; //获取本月1号的周值 function oneyearoneday(dateObj){ var oneyear = new Date(); var year=dateObj.getFullYear(); var month=dateObj.getMonth();//0是12月 oneyear.setFullYear(year); oneyear.setMonth(month);//0是12月 oneyear.setDate(1); return oneyear.getDay(); }; //当前是几 function nowday(dateObj){ return dateObj.getDate(); }; //获取本月总日数方法 function alldays(dateObj){ var year=dateObj.getFullYear(); var month=dateObj.getMonth(); if(isLeapYear(year)){//闰年 switch(month) { case 0: return "31"; break; case 1: return "29"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }else{//平年 switch(month) { case 0: return "31"; break; case 1: return "28"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }; }; //闰年判断函数 function isLeapYear(year){ if( (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)){ return true; }else{ return false; }; }; //月份转化方法 function toyear(dateObj){ var month=dateObj.getMonth() switch(month) { case 0: return "1"; break; case 1: return "2"; break; case 2: return "3"; break; case 3: return "4"; break; case 4: return "5"; break; case 5: return "6"; break; case 6: return "7"; break; case 7: return "8"; break; case 8: return "9"; break; case 9: return "10"; break; case 10: return "11"; break; case 11: return "12"; break; default: }; }; }; </script> </html>
我们补全下一月处理,测试:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>date</title> <style type="text/css"> #box{width:350px;} #title{width:350px; height:50px;} #month{ float:left;width:60px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #year{float:left;width:80px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #week{ width:350px;height:50px;} #week div{ float:left; width:48px; height:48px; margin:1px; background:#C90; color:#fff; text-align:center; line-height:48px; cursor:pointer;} #con{ width:350px;} #con div{ float:left; width:48px; height:48px; margin:1px; background:#999; color:#333; text-align:center; line-height:48px; cursor:pointer;} #con div.now{background:#09F; color:#fff;} #prevmonth,#nextmonth{float:left;width:50px; height:50px;text-align:center;cursor:pointer;line-height:50px;} </style> </head> <body> <div id="box"> <div id="title"> <div id="prevmonth"><</div> <div id="month"></div> <div id="year"></div> <div id="nextmonth">></div> </div> <div id="week"> <div>日</div> <div>一</div> <div>二</div> <div>三</div> <div>四</div> <div>五</div> <div>六</div> </div> <div id="con"></div> </div> </body> <script type="text/javascript"> window.onload=function(){ //===================get ele=============================== var omonth=document.getElementById("month"); var oyear=document.getElementById("year"); var con=document.getElementById("con"); var prevmonth=document.getElementById("prevmonth"); var nextmonth=document.getElementById("nextmonth"); //===================set year month=============================== //默认时间对象 var dateObj = new Date(); //动态控制 prevmonth.onclick=function(){//上一月 var ddm=null; var ddy=null; if((dateObj.getMonth()-1)==-1){ ddm=11; ddy=dateObj.getFullYear()-1; }else{ ddm=dateObj.getMonth()-1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextmonth.onclick=function(){//下一月 var ddm=null; var ddy=null; if((dateObj.getMonth()+1)==12){ ddm=0; ddy=dateObj.getFullYear()+1; }else{ ddm=dateObj.getMonth()+1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; //年月获取 var year=dateObj.getFullYear(); var month=toyear(dateObj);//0是12月 //月年的显示 omonth.innerHTML=month+"月"; oyear.innerHTML=year+"年"; //===================set day=============================== //获取本月1号的周值 var oneweek=oneyearoneday(dateObj); //本月总日数 var alld=alldays(dateObj); //当前是几 var nowd=nowday(dateObj); //初始化显示本月信息 init(oneweek,alld,nowd); //===================function=============================== //初始化日期显示方法 function remove(){ con.innerHTML=""; }; function init(oneweek,alld,nowd){ for(var i=1;i<=oneweek;i++){//留空 var eday=document.createElement("div"); eday.innerHTML=""; con.appendChild(eday); }; for(var i=1;i<=alld;i++){//正常区域 var eday=document.createElement("div"); if(i==nowd){ eday.innerHTML=i; eday.className="now"; con.appendChild(eday); }else{ eday.innerHTML=i; con.appendChild(eday); }; }; }; //获取本月1号的周值 function oneyearoneday(dateObj){ var oneyear = new Date(); var year=dateObj.getFullYear(); var month=dateObj.getMonth();//0是12月 oneyear.setFullYear(year); oneyear.setMonth(month);//0是12月 oneyear.setDate(1); return oneyear.getDay(); }; //当前是几 function nowday(dateObj){ return dateObj.getDate(); }; //获取本月总日数方法 function alldays(dateObj){ var year=dateObj.getFullYear(); var month=dateObj.getMonth(); if(isLeapYear(year)){//闰年 switch(month) { case 0: return "31"; break; case 1: return "29"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }else{//平年 switch(month) { case 0: return "31"; break; case 1: return "28"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }; }; //闰年判断函数 function isLeapYear(year){ if( (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)){ return true; }else{ return false; }; }; //月份转化方法 function toyear(dateObj){ var month=dateObj.getMonth() switch(month) { case 0: return "1"; break; case 1: return "2"; break; case 2: return "3"; break; case 3: return "4"; break; case 4: return "5"; break; case 5: return "6"; break; case 6: return "7"; break; case 7: return "8"; break; case 8: return "9"; break; case 9: return "10"; break; case 10: return "11"; break; case 11: return "12"; break; default: }; }; }; </script> </html>
三.开发上一年下一年控制程序
和月份处理类似,我直接粘贴代码了:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>date</title> <style type="text/css"> #box{width:350px;} #title{width:350px; height:50px;} #month{ float:left;width:60px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #year{float:left;width:80px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #week{ width:350px;height:50px;} #week div{ float:left; width:48px; height:48px; margin:1px; background:#C90; color:#fff; text-align:center; line-height:48px; cursor:pointer;} #con{ width:350px;} #con div{ float:left; width:48px; height:48px; margin:1px; background:#999; color:#333; text-align:center; line-height:48px; cursor:pointer;} #con div.now{background:#09F; color:#fff;} #prevmonth,#nextmonth,#prevyear,#nextyear{float:left;width:50px; height:50px;text-align:center;cursor:pointer;line-height:50px;} </style> </head> <body> <div id="box"> <div id="title"> <div id="prevyear"><<</div> <div id="prevmonth"><</div> <div id="month"></div> <div id="year"></div> <div id="nextmonth">></div> <div id="nextyear">>></div> </div> <div id="week"> <div>日</div> <div>一</div> <div>二</div> <div>三</div> <div>四</div> <div>五</div> <div>六</div> </div> <div id="con"></div> </div> </body> <script type="text/javascript"> window.onload=function(){ //===================get ele=============================== var omonth=document.getElementById("month"); var oyear=document.getElementById("year"); var con=document.getElementById("con"); var prevmonth=document.getElementById("prevmonth"); var nextmonth=document.getElementById("nextmonth"); var prevyear=document.getElementById("prevyear"); var nextyear=document.getElementById("nextyear"); //===================set year month=============================== //默认时间对象 var dateObj = new Date(); //动态控制 prevmonth.onclick=function(){//上一月 var ddm=null; var ddy=null; if((dateObj.getMonth()-1)==-1){ ddm=11; ddy=dateObj.getFullYear()-1; }else{ ddm=dateObj.getMonth()-1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextmonth.onclick=function(){//下一月 var ddm=null; var ddy=null; if((dateObj.getMonth()+1)==12){ ddm=0; ddy=dateObj.getFullYear()+1; }else{ ddm=dateObj.getMonth()+1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; prevyear.onclick=function(){//上一年 var ddy=dateObj.getFullYear()-1; dateObj.setFullYear(ddy); oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextyear.onclick=function(){//下一年 var ddy=dateObj.getFullYear()+1; dateObj.setFullYear(ddy); oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; //年月获取 var year=dateObj.getFullYear(); var month=toyear(dateObj);//0是12月 //月年的显示 omonth.innerHTML=month+"月"; oyear.innerHTML=year+"年"; //===================set day=============================== //获取本月1号的周值 var oneweek=oneyearoneday(dateObj); //本月总日数 var alld=alldays(dateObj); //当前是几 var nowd=nowday(dateObj); //初始化显示本月信息 init(oneweek,alld,nowd); //===================function=============================== //初始化日期显示方法 function remove(){ con.innerHTML=""; }; function init(oneweek,alld,nowd){ for(var i=1;i<=oneweek;i++){//留空 var eday=document.createElement("div"); eday.innerHTML=""; con.appendChild(eday); }; for(var i=1;i<=alld;i++){//正常区域 var eday=document.createElement("div"); if(i==nowd){ eday.innerHTML=i; eday.className="now"; con.appendChild(eday); }else{ eday.innerHTML=i; con.appendChild(eday); }; }; }; //获取本月1号的周值 function oneyearoneday(dateObj){ var oneyear = new Date(); var year=dateObj.getFullYear(); var month=dateObj.getMonth();//0是12月 oneyear.setFullYear(year); oneyear.setMonth(month);//0是12月 oneyear.setDate(1); return oneyear.getDay(); }; //当前是几 function nowday(dateObj){ return dateObj.getDate(); }; //获取本月总日数方法 function alldays(dateObj){ var year=dateObj.getFullYear(); var month=dateObj.getMonth(); if(isLeapYear(year)){//闰年 switch(month) { case 0: return "31"; break; case 1: return "29"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }else{//平年 switch(month) { case 0: return "31"; break; case 1: return "28"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }; }; //闰年判断函数 function isLeapYear(year){ if( (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)){ return true; }else{ return false; }; }; //月份转化方法 function toyear(dateObj){ var month=dateObj.getMonth() switch(month) { case 0: return "1"; break; case 1: return "2"; break; case 2: return "3"; break; case 3: return "4"; break; case 4: return "5"; break; case 5: return "6"; break; case 6: return "7"; break; case 7: return "8"; break; case 8: return "9"; break; case 9: return "10"; break; case 10: return "11"; break; case 11: return "12"; break; default: }; }; }; </script> </html>
到这一步,我们的日历控件已经出具模型了。
四.返回当前时期
我们应该加一个按钮,不管处在那一年,我们都可以点击后立即回到今时今日:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>date</title> <style type="text/css"> *{ margin:0; padding:0;} a{ text-decoration:none; outline:none;} body a{outline:none;blr:expression(this.onFocus=this.blur());} img{ border:none;} ul{ list-style:none;} body{ color:#666666; font-size:14px; font-family:"微软雅黑"; background-color:#fff; height:100%; overflow-y:scroll;*overflow-y:inherit;} /*html{ height:100%;}*/ .clearfix:after{ content:"."; display:block; height:0; clear:both; overflow:hidden;} .clearfix{ zoom:1;} #box{width:350px;} #title{width:350px; height:50px;} #month{ float:left;width:60px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #year{float:left;width:80px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #week{ width:350px;height:50px;} #week div{ float:left; width:48px; height:48px; margin:1px; background:#C90; color:#fff; text-align:center; line-height:48px; cursor:pointer;} #con{ width:350px;} #con div{ float:left; width:48px; height:48px; margin:1px; background:#999; color:#333; text-align:center; line-height:48px; cursor:pointer;} #con div.now{background:#09F; color:#fff;} #prevmonth,#nextmonth,#prevyear,#nextyear{float:left;width:50px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #btns{width:350px; height:40px;} #nowtime{ float:left; margin:5px; height:30px; line-height:30px; padding:0 5px; background:#09F; cursor:pointer; border-radius:5px;} #nowtime:hover{background:#ddd;} </style> </head> <body> <div id="box"> <div id="title"> <div id="prevyear"><<</div> <div id="prevmonth"><</div> <div id="month"></div> <div id="year"></div> <div id="nextmonth">></div> <div id="nextyear">>></div> </div> <div id="week"> <div>日</div> <div>一</div> <div>二</div> <div>三</div> <div>四</div> <div>五</div> <div>六</div> </div> <div id="con" class="clearfix"></div> <div id="btns"> <div id="nowtime">当前时间</div> </div> </div> </body> <script type="text/javascript"> window.onload=function(){ //===================get ele=============================== var omonth=document.getElementById("month"); var oyear=document.getElementById("year"); var con=document.getElementById("con"); var prevmonth=document.getElementById("prevmonth"); var nextmonth=document.getElementById("nextmonth"); var prevyear=document.getElementById("prevyear"); var nextyear=document.getElementById("nextyear"); var nowtime=document.getElementById("nowtime"); //===================set year month=============================== //默认时间对象 var dateObj = new Date(); //动态控制 prevmonth.onclick=function(){//上一月 var ddm=null; var ddy=null; if((dateObj.getMonth()-1)==-1){ ddm=11; ddy=dateObj.getFullYear()-1; }else{ ddm=dateObj.getMonth()-1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextmonth.onclick=function(){//下一月 var ddm=null; var ddy=null; if((dateObj.getMonth()+1)==12){ ddm=0; ddy=dateObj.getFullYear()+1; }else{ ddm=dateObj.getMonth()+1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; prevyear.onclick=function(){//上一年 var ddy=dateObj.getFullYear()-1; dateObj.setFullYear(ddy); oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextyear.onclick=function(){//下一年 var ddy=dateObj.getFullYear()+1; dateObj.setFullYear(ddy); oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; //返回到今时今日 nowtime.onclick=function(){ var dddate=new Date(); var ddm=dddate.getMonth(); var ddy=dddate.getFullYear(); dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; //年月获取 var year=dateObj.getFullYear(); var month=toyear(dateObj);//0是12月 //月年的显示 omonth.innerHTML=month+"月"; oyear.innerHTML=year+"年"; //===================set day=============================== //获取本月1号的周值 var oneweek=oneyearoneday(dateObj); //本月总日数 var alld=alldays(dateObj); //当前是几 var nowd=nowday(dateObj); //初始化显示本月信息 init(oneweek,alld,nowd); //===================function=============================== //初始化日期显示方法 function remove(){ con.innerHTML=""; }; function init(oneweek,alld,nowd){ for(var i=1;i<=oneweek;i++){//留空 var eday=document.createElement("div"); eday.innerHTML=""; con.appendChild(eday); }; for(var i=1;i<=alld;i++){//正常区域 var eday=document.createElement("div"); if(i==nowd){ eday.innerHTML=i; eday.className="now"; con.appendChild(eday); }else{ eday.innerHTML=i; con.appendChild(eday); }; }; }; //获取本月1号的周值 function oneyearoneday(dateObj){ var oneyear = new Date(); var year=dateObj.getFullYear(); var month=dateObj.getMonth();//0是12月 oneyear.setFullYear(year); oneyear.setMonth(month);//0是12月 oneyear.setDate(1); return oneyear.getDay(); }; //当前是几 function nowday(dateObj){ return dateObj.getDate(); }; //获取本月总日数方法 function alldays(dateObj){ var year=dateObj.getFullYear(); var month=dateObj.getMonth(); if(isLeapYear(year)){//闰年 switch(month) { case 0: return "31"; break; case 1: return "29"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }else{//平年 switch(month) { case 0: return "31"; break; case 1: return "28"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }; }; //闰年判断函数 function isLeapYear(year){ if( (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)){ return true; }else{ return false; }; }; //月份转化方法 function toyear(dateObj){ var month=dateObj.getMonth() switch(month) { case 0: return "1"; break; case 1: return "2"; break; case 2: return "3"; break; case 3: return "4"; break; case 4: return "5"; break; case 5: return "6"; break; case 6: return "7"; break; case 7: return "8"; break; case 8: return "9"; break; case 9: return "10"; break; case 10: return "11"; break; case 11: return "12"; break; default: }; }; }; </script> </html>
五.与输入文本框绑定
我的时间控件默认是不可见的在input获取焦点时才会显示:
默认隐藏,最简单就是直接none掉,input获取焦点我们blcok,位置利用绝对定位,获取input相对页面的left和top赋值给box即可:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>date</title> <style type="text/css"> *{ margin:0; padding:0;} a{ text-decoration:none; outline:none;} body a{outline:none;blr:expression(this.onFocus=this.blur());} img{ border:none;} ul{ list-style:none;} body{ color:#666666; font-size:14px; font-family:"微软雅黑"; background-color:#fff; height:100%; overflow-y:scroll;*overflow-y:inherit;} /*html{ height:100%;}*/ .clearfix:after{ content:"."; display:block; height:0; clear:both; overflow:hidden;} .clearfix{ zoom:1;} #box{width:350px; position:absolute;} #title{width:350px; height:50px;} #month{ float:left;width:60px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #year{float:left;width:80px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #week{ width:350px;height:50px;} #week div{ float:left; width:48px; height:48px; margin:1px; background:#C90; color:#fff; text-align:center; line-height:48px; cursor:pointer;} #con{ width:350px;} #con div{ float:left; width:48px; height:48px; margin:1px; background:#CCC; color:#333; text-align:center; line-height:48px; cursor:pointer;} #con .edate{background:#999;} #con .edate:hover{background:#09F; color:#fff;} #con div.now{background:#09F; color:#fff;} #prevmonth,#nextmonth,#prevyear,#nextyear{float:left;width:50px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #btns{width:350px; height:40px;} #nowtime{ float:left; margin:5px; height:30px; line-height:30px; padding:0 5px; background:#09F; cursor:pointer; border-radius:5px;} #nowtime:hover{background:#ddd;} </style> </head> <body> <input type="text" id="date" value="" style="height:40px; line-height:40px; margin:200px;"/> <div id="box" style="display:none;"> <div id="title"> <div id="prevyear"><<</div> <div id="prevmonth"><</div> <div id="month"></div> <div id="year"></div> <div id="nextmonth">></div> <div id="nextyear">>></div> </div> <div id="week"> <div>日</div> <div>一</div> <div>二</div> <div>三</div> <div>四</div> <div>五</div> <div>六</div> </div> <div id="con" class="clearfix"></div> <div id="btns"> <div id="nowtime">当前时间</div> </div> </div> </body> <script type="text/javascript"> window.onload=function(){ //===================get ele=============================== var omonth=document.getElementById("month"); var oyear=document.getElementById("year"); var con=document.getElementById("con"); var prevmonth=document.getElementById("prevmonth"); var nextmonth=document.getElementById("nextmonth"); var prevyear=document.getElementById("prevyear"); var nextyear=document.getElementById("nextyear"); var nowtime=document.getElementById("nowtime"); var date=document.getElementById("date"); var box=document.getElementById("box"); //===================show date=============================== date.onfocus=function(){//显示控件 var datel=this.getBoundingClientRect().left; var datet=this.getBoundingClientRect().top+40; box.style.left=datel+"px"; box.style.top=datet+"px"; box.style.display="block"; }; con.onclick=function(event){ if(event.target.tagName=="DIV" && event.target.nodeType=="1" && hasclass(event.target.className,"edate")){ date.value=""; date.value=dateObj.getFullYear()+"-"+toyear(dateObj)+"-"+event.target.innerHTML; box.style.display="none"; }; }; //===================set year month=============================== //默认时间对象 var dateObj = new Date(); //动态控制 prevmonth.onclick=function(){//上一月 var ddm=null; var ddy=null; if((dateObj.getMonth()-1)==-1){ ddm=11; ddy=dateObj.getFullYear()-1; }else{ ddm=dateObj.getMonth()-1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextmonth.onclick=function(){//下一月 var ddm=null; var ddy=null; if((dateObj.getMonth()+1)==12){ ddm=0; ddy=dateObj.getFullYear()+1; }else{ ddm=dateObj.getMonth()+1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; prevyear.onclick=function(){//上一年 var ddy=dateObj.getFullYear()-1; dateObj.setFullYear(ddy); oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextyear.onclick=function(){//下一年 var ddy=dateObj.getFullYear()+1; dateObj.setFullYear(ddy); oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; //返回到今时今日 nowtime.onclick=function(){ var dddate=new Date(); var ddm=dddate.getMonth(); var ddy=dddate.getFullYear(); dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; //年月获取 var year=dateObj.getFullYear(); var month=toyear(dateObj);//0是12月 //月年的显示 omonth.innerHTML=month+"月"; oyear.innerHTML=year+"年"; //===================set day=============================== //获取本月1号的周值 var oneweek=oneyearoneday(dateObj); //本月总日数 var alld=alldays(dateObj); //当前是几 var nowd=nowday(dateObj); //初始化显示本月信息 init(oneweek,alld,nowd); //===================function=============================== //有无指定类名的判断 function hasclass(str,cla){ var i=str.search(cla); if(i==-1){ return false; }else{ return true; }; }; //初始化日期显示方法 function remove(){ con.innerHTML=""; }; function init(oneweek,alld,nowd){ for(var i=1;i<=oneweek;i++){//留空 var eday=document.createElement("div"); eday.innerHTML=""; con.appendChild(eday); }; for(var i=1;i<=alld;i++){//正常区域 var eday=document.createElement("div"); if(i==nowd){ eday.innerHTML=i; eday.className="now edate"; con.appendChild(eday); }else{ eday.innerHTML=i; eday.className="edate"; con.appendChild(eday); }; }; }; //获取本月1号的周值 function oneyearoneday(dateObj){ var oneyear = new Date(); var year=dateObj.getFullYear(); var month=dateObj.getMonth();//0是12月 oneyear.setFullYear(year); oneyear.setMonth(month);//0是12月 oneyear.setDate(1); return oneyear.getDay(); }; //当前是几 function nowday(dateObj){ return dateObj.getDate(); }; //获取本月总日数方法 function alldays(dateObj){ var year=dateObj.getFullYear(); var month=dateObj.getMonth(); if(isLeapYear(year)){//闰年 switch(month) { case 0: return "31"; break; case 1: return "29"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }else{//平年 switch(month) { case 0: return "31"; break; case 1: return "28"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }; }; //闰年判断函数 function isLeapYear(year){ if( (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)){ return true; }else{ return false; }; }; //月份转化方法 function toyear(dateObj){ var month=dateObj.getMonth() switch(month) { case 0: return "1"; break; case 1: return "2"; break; case 2: return "3"; break; case 3: return "4"; break; case 4: return "5"; break; case 5: return "6"; break; case 6: return "7"; break; case 7: return "8"; break; case 8: return "9"; break; case 9: return "10"; break; case 10: return "11"; break; case 11: return "12"; break; default: }; }; }; </script> </html>
在点击日历我加入了三个判断,都是保证正确值的点击处理
六.清空操作
这个就非常简单了,我们加入清空按钮:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>date</title> <style type="text/css"> *{ margin:0; padding:0;} a{ text-decoration:none; outline:none;} body a{outline:none;blr:expression(this.onFocus=this.blur());} img{ border:none;} ul{ list-style:none;} body{ color:#666666; font-size:14px; font-family:"微软雅黑"; background-color:#fff; height:100%; overflow-y:scroll;*overflow-y:inherit;} /*html{ height:100%;}*/ .clearfix:after{ content:"."; display:block; height:0; clear:both; overflow:hidden;} .clearfix{ zoom:1;} #box{width:350px; position:absolute;} #title{width:350px; height:50px;} #month{ float:left;width:60px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #year{float:left;width:80px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #week{ width:350px;height:50px;} #week div{ float:left; width:48px; height:48px; margin:1px; background:#C90; color:#fff; text-align:center; line-height:48px; cursor:pointer;} #con{ width:350px;} #con div{ float:left; width:48px; height:48px; margin:1px; background:#CCC; color:#333; text-align:center; line-height:48px; cursor:pointer;} #con .edate{background:#999;} #con .edate:hover{background:#09F; color:#fff;} #con div.now{background:#09F; color:#fff;} #prevmonth,#nextmonth,#prevyear,#nextyear{float:left;width:50px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #btns{width:350px; height:40px;} #nowtime{ float:left; margin:5px; height:30px; line-height:30px; padding:0 5px; background:#09F; cursor:pointer; border-radius:5px;} #nowtime:hover{background:#ddd;} #cleartime{float:left; margin:5px; height:30px; line-height:30px; padding:0 5px; background:#09F; cursor:pointer; border-radius:5px;} </style> </head> <body> <input type="text" id="date" value="" style="height:40px; line-height:40px; margin:200px;"/> <div id="box" style="display:none;"> <div id="title"> <div id="prevyear"><<</div> <div id="prevmonth"><</div> <div id="month"></div> <div id="year"></div> <div id="nextmonth">></div> <div id="nextyear">>></div> </div> <div id="week"> <div>日</div> <div>一</div> <div>二</div> <div>三</div> <div>四</div> <div>五</div> <div>六</div> </div> <div id="con" class="clearfix"></div> <div id="btns"> <div id="nowtime">当前时间</div> <div id="cleartime">清空</div> </div> </div> </body> <script type="text/javascript"> window.onload=function(){ //===================get ele=============================== var omonth=document.getElementById("month"); var oyear=document.getElementById("year"); var con=document.getElementById("con"); var prevmonth=document.getElementById("prevmonth"); var nextmonth=document.getElementById("nextmonth"); var prevyear=document.getElementById("prevyear"); var nextyear=document.getElementById("nextyear"); var nowtime=document.getElementById("nowtime"); var date=document.getElementById("date"); var box=document.getElementById("box"); var cleartime=document.getElementById("cleartime"); //===================show date=============================== date.onfocus=function(){//显示控件 var datel=this.getBoundingClientRect().left; var datet=this.getBoundingClientRect().top+40; box.style.left=datel+"px"; box.style.top=datet+"px"; box.style.display="block"; }; con.onclick=function(event){ if(event.target.tagName=="DIV" && event.target.nodeType=="1" && hasclass(event.target.className,"edate")){ date.value=""; date.value=dateObj.getFullYear()+"-"+toyear(dateObj)+"-"+event.target.innerHTML; box.style.display="none"; }; }; cleartime.onclick=function(event){ date.value=""; }; //===================set year month=============================== //默认时间对象 var dateObj = new Date(); //动态控制 prevmonth.onclick=function(){//上一月 var ddm=null; var ddy=null; if((dateObj.getMonth()-1)==-1){ ddm=11; ddy=dateObj.getFullYear()-1; }else{ ddm=dateObj.getMonth()-1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextmonth.onclick=function(){//下一月 var ddm=null; var ddy=null; if((dateObj.getMonth()+1)==12){ ddm=0; ddy=dateObj.getFullYear()+1; }else{ ddm=dateObj.getMonth()+1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; prevyear.onclick=function(){//上一年 var ddy=dateObj.getFullYear()-1; dateObj.setFullYear(ddy); oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextyear.onclick=function(){//下一年 var ddy=dateObj.getFullYear()+1; dateObj.setFullYear(ddy); oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; //返回到今时今日 nowtime.onclick=function(){ var dddate=new Date(); var ddm=dddate.getMonth(); var ddy=dddate.getFullYear(); dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; //年月获取 var year=dateObj.getFullYear(); var month=toyear(dateObj);//0是12月 //月年的显示 omonth.innerHTML=month+"月"; oyear.innerHTML=year+"年"; //===================set day=============================== //获取本月1号的周值 var oneweek=oneyearoneday(dateObj); //本月总日数 var alld=alldays(dateObj); //当前是几 var nowd=nowday(dateObj); //初始化显示本月信息 init(oneweek,alld,nowd); //===================function=============================== //有无指定类名的判断 function hasclass(str,cla){ var i=str.search(cla); if(i==-1){ return false; }else{ return true; }; }; //初始化日期显示方法 function remove(){ con.innerHTML=""; }; function init(oneweek,alld,nowd){ for(var i=1;i<=oneweek;i++){//留空 var eday=document.createElement("div"); eday.innerHTML=""; con.appendChild(eday); }; for(var i=1;i<=alld;i++){//正常区域 var eday=document.createElement("div"); if(i==nowd){ eday.innerHTML=i; eday.className="now edate"; con.appendChild(eday); }else{ eday.innerHTML=i; eday.className="edate"; con.appendChild(eday); }; }; }; //获取本月1号的周值 function oneyearoneday(dateObj){ var oneyear = new Date(); var year=dateObj.getFullYear(); var month=dateObj.getMonth();//0是12月 oneyear.setFullYear(year); oneyear.setMonth(month);//0是12月 oneyear.setDate(1); return oneyear.getDay(); }; //当前是几 function nowday(dateObj){ return dateObj.getDate(); }; //获取本月总日数方法 function alldays(dateObj){ var year=dateObj.getFullYear(); var month=dateObj.getMonth(); if(isLeapYear(year)){//闰年 switch(month) { case 0: return "31"; break; case 1: return "29"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }else{//平年 switch(month) { case 0: return "31"; break; case 1: return "28"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }; }; //闰年判断函数 function isLeapYear(year){ if( (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)){ return true; }else{ return false; }; }; //月份转化方法 function toyear(dateObj){ var month=dateObj.getMonth() switch(month) { case 0: return "1"; break; case 1: return "2"; break; case 2: return "3"; break; case 3: return "4"; break; case 4: return "5"; break; case 5: return "6"; break; case 6: return "7"; break; case 7: return "8"; break; case 8: return "9"; break; case 9: return "10"; break; case 10: return "11"; break; case 11: return "12"; break; default: }; }; }; </script> </html>
七.生产完整代码
对比参照控件,我们几乎没有差别了。
其实一个页面可以有多个input,这时候我们的时间控件结构应该通过js插入生成,当然更多的扩展等你来加入:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>date</title> <style type="text/css"> *{ margin:0; padding:0;} a{ text-decoration:none; outline:none;} body a{outline:none;blr:expression(this.onFocus=this.blur());} img{ border:none;} ul{ list-style:none;} body{ color:#666666; font-size:14px; font-family:"微软雅黑"; background-color:#fff; height:100%; overflow-y:scroll;*overflow-y:inherit;} /*html{ height:100%;}*/ .clearfix:after{ content:"."; display:block; height:0; clear:both; overflow:hidden;} .clearfix{ zoom:1;} #box{width:350px; position:absolute;} #title{width:350px; height:50px;} #month{ float:left;width:60px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #year{float:left;width:80px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #week{ width:350px;height:50px;} #week div{ float:left; width:48px; height:48px; margin:1px; background:#C90; color:#fff; text-align:center; line-height:48px; cursor:pointer;} #con{ width:350px;} #con div{ float:left; width:48px; height:48px; margin:1px; background:#CCC; color:#333; text-align:center; line-height:48px; cursor:pointer;} #con .edate{background:#999;} #con .edate:hover{background:#09F; color:#fff;} #con div.now{background:#09F; color:#fff;} #prevmonth,#nextmonth,#prevyear,#nextyear{float:left;width:50px; height:50px;text-align:center;cursor:pointer;line-height:50px;} #btns{width:350px; height:40px;} #nowtime{ float:left; margin:5px; height:30px; line-height:30px; padding:0 5px; background:#09F; cursor:pointer; border-radius:5px;} #nowtime:hover{background:#ddd;} #cleartime{float:left; margin:5px; height:30px; line-height:30px; padding:0 5px; background:#09F; cursor:pointer; border-radius:5px;} </style> </head> <body> <input type="text" id="date" value="" style="height:40px; line-height:40px; margin:200px;"/> <div id="box" style="display:none;"> <div id="title"> <div id="prevyear"><<</div> <div id="prevmonth"><</div> <div id="month"></div> <div id="year"></div> <div id="nextmonth">></div> <div id="nextyear">>></div> </div> <div id="week"> <div>日</div> <div>一</div> <div>二</div> <div>三</div> <div>四</div> <div>五</div> <div>六</div> </div> <div id="con" class="clearfix"></div> <div id="btns"> <div id="nowtime">当前时间</div> <div id="cleartime">清空</div> </div> </div> </body> <script type="text/javascript"> window.onload=function(){ //===================get ele=============================== var omonth=document.getElementById("month"); var oyear=document.getElementById("year"); var con=document.getElementById("con"); var prevmonth=document.getElementById("prevmonth"); var nextmonth=document.getElementById("nextmonth"); var prevyear=document.getElementById("prevyear"); var nextyear=document.getElementById("nextyear"); var nowtime=document.getElementById("nowtime"); var date=document.getElementById("date"); var box=document.getElementById("box"); var cleartime=document.getElementById("cleartime"); //===================show date=============================== date.onfocus=function(){//显示控件 var datel=this.getBoundingClientRect().left; var datet=this.getBoundingClientRect().top+40; box.style.left=datel+"px"; box.style.top=datet+"px"; box.style.display="block"; }; con.onclick=function(event){ if(event.target.tagName=="DIV" && event.target.nodeType=="1" && hasclass(event.target.className,"edate")){ date.value=""; date.value=dateObj.getFullYear()+"-"+toyear(dateObj)+"-"+event.target.innerHTML; box.style.display="none"; }; }; cleartime.onclick=function(event){ date.value=""; }; //===================set year month=============================== //默认时间对象 var dateObj = new Date(); //动态控制 prevmonth.onclick=function(){//上一月 var ddm=null; var ddy=null; if((dateObj.getMonth()-1)==-1){ ddm=11; ddy=dateObj.getFullYear()-1; }else{ ddm=dateObj.getMonth()-1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextmonth.onclick=function(){//下一月 var ddm=null; var ddy=null; if((dateObj.getMonth()+1)==12){ ddm=0; ddy=dateObj.getFullYear()+1; }else{ ddm=dateObj.getMonth()+1; ddy=dateObj.getFullYear(); }; dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; prevyear.onclick=function(){//上一年 var ddy=dateObj.getFullYear()-1; dateObj.setFullYear(ddy); oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; nextyear.onclick=function(){//下一年 var ddy=dateObj.getFullYear()+1; dateObj.setFullYear(ddy); oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; //返回到今时今日 nowtime.onclick=function(){ var dddate=new Date(); var ddm=dddate.getMonth(); var ddy=dddate.getFullYear(); dateObj.setFullYear(ddy); dateObj.setMonth(ddm); omonth.innerHTML=toyear(dateObj)+"月"; oyear.innerHTML=dateObj.getFullYear()+"年"; remove(); oneweek=oneyearoneday(dateObj); alld=alldays(dateObj); nowd=nowday(dateObj); init(oneweek,alld,nowd); }; //年月获取 var year=dateObj.getFullYear(); var month=toyear(dateObj);//0是12月 //月年的显示 omonth.innerHTML=month+"月"; oyear.innerHTML=year+"年"; //===================set day=============================== //获取本月1号的周值 var oneweek=oneyearoneday(dateObj); //本月总日数 var alld=alldays(dateObj); //当前是几 var nowd=nowday(dateObj); //初始化显示本月信息 init(oneweek,alld,nowd); //===================function=============================== //有无指定类名的判断 function hasclass(str,cla){ var i=str.search(cla); if(i==-1){ return false; }else{ return true; }; }; //初始化日期显示方法 function remove(){ con.innerHTML=""; }; function init(oneweek,alld,nowd){ for(var i=1;i<=oneweek;i++){//留空 var eday=document.createElement("div"); eday.innerHTML=""; con.appendChild(eday); }; for(var i=1;i<=alld;i++){//正常区域 var eday=document.createElement("div"); if(i==nowd){ eday.innerHTML=i; eday.className="now edate"; con.appendChild(eday); }else{ eday.innerHTML=i; eday.className="edate"; con.appendChild(eday); }; }; }; //获取本月1号的周值 function oneyearoneday(dateObj){ var oneyear = new Date(); var year=dateObj.getFullYear(); var month=dateObj.getMonth();//0是12月 oneyear.setFullYear(year); oneyear.setMonth(month);//0是12月 oneyear.setDate(1); return oneyear.getDay(); }; //当前是几 function nowday(dateObj){ return dateObj.getDate(); }; //获取本月总日数方法 function alldays(dateObj){ var year=dateObj.getFullYear(); var month=dateObj.getMonth(); if(isLeapYear(year)){//闰年 switch(month) { case 0: return "31"; break; case 1: return "29"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }else{//平年 switch(month) { case 0: return "31"; break; case 1: return "28"; break; //2月 case 2: return "31"; break; case 3: return "30"; break; case 4: return "31"; break; case 5: return "30"; break; case 6: return "31"; break; case 7: return "31"; break; case 8: return "30"; break; case 9: return "31"; break; case 10: return "30"; break; case 11: return "31"; break; default: }; }; }; //闰年判断函数 function isLeapYear(year){ if( (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)){ return true; }else{ return false; }; }; //月份转化方法 function toyear(dateObj){ var month=dateObj.getMonth() switch(month) { case 0: return "1"; break; case 1: return "2"; break; case 2: return "3"; break; case 3: return "4"; break; case 4: return "5"; break; case 5: return "6"; break; case 6: return "7"; break; case 7: return "8"; break; case 8: return "9"; break; case 9: return "10"; break; case 10: return "11"; break; case 11: return "12"; break; default: }; }; }; </script> </html>