<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>日历控件</title>
<style type="text/css">
#calendar{ width:142px; padding:5px; height:198px; background:#FCF; -webkit-user-select:none; position:absolute;}
#calendar div.week span,#calendar span.date{ width:14px; font-size:12px; height:20px; text-align:center; line-height:20px; border:1px solid #ccc; margin:2px; float:left;}
#calendar span.date{ background:#fff; cursor:pointer;}
#calendar div.week{ clear:both; overflow:hidden;}
#calendar div.week span{ font-weight:bold;}
#calendar h6{ margin:0; padding:0; font-size:14px; line-height:20px; text-align:center; position:relative; overflow:hidden; height:20px;}
#prev,#next{ width:45px; text-align:center; position:absolute; color:blue; cursor:pointer; }
#prev{ left:0;}
#next{ right:0;}
</style>
</head> <body>
<input type="text" value="请输入……" id="input1">
</body>
</html>
<script>
input1.onfocus=function(){
createCalendar(this);
}
//input1.onfocus=new Function("this;calendar(this)");
function createCalendar(ele){
var obj=offset(ele);
var l=obj.left;
var t=obj.top;
var oCalendar=document.getElementById('calendar');
if(oCalendar)return;
oCalendar=document.createElement('div');
oCalendar.style.top=t+'px';
oCalendar.style.left=l+ele.offsetWidth+5+'px';
oCalendar.id="calendar";
var oTitle=document.createElement('h6');
oCalendar.appendChild(oTitle);
oTitle.className="title";
//创建上prev按钮
var oPrev=document.createElement('span');
oPrev.id="prev";
oPrev.innerHTML="prev";
oTitle.appendChild(oPrev);
oPrev.onclick=function(){activeCalendar(--month);}
//创建中间titleContent
var oTitleContent=document.createElement('span');
//oTitleContent.id="titleContent";
oTitle.appendChild(oTitleContent);
var d=new Date;
var year=d.getFullYear();
var month=d.getMonth();
var m=month;
//oTitleContent.innerHTML=year+"年"+(month+1)+"月"
//创建next按钮
var oNext=document.createElement('span');
oNext.id="next";
oNext.innerHTML="next";
oNext.onclick=function(){activeCalendar(++month);}
oTitle.appendChild(oNext);
//创建周的导航栏
var aWeek=["日","一","二","三","四","五","六"];
var oWeek=document.createElement('div');
oWeek.className="week";
oCalendar.appendChild(oWeek); for(var i=0;i<7;i++){
var oSpan=document.createElement('span');
oSpan.innerHTML=aWeek[i];
oWeek.appendChild(oSpan);
}
//创建日期的容器
var oDateContent=document.createElement('div');
oDateContent.id="dateContent";
oCalendar.appendChild(oDateContent);
document.body.appendChild(oCalendar);
activeCalendar(month);
function activeCalendar(month){
oDateContent.innerHTML="";
var d1=new Date(year,month,1);
var diff=1-d1.getDay();
month=d1.getMonth();
if(diff==1)diff=-6;
d1.setDate(diff); oTitleContent.innerHTML=d1.getFullYear()+"年"+(d1.getMonth()+2)+"月"
for(var i=0;i<42;i++){
var oSpan=document.createElement('span');
oSpan.className="date";
var currentDate=d1.getDate();
oSpan.innerHTML=currentDate;
oSpan.curDate=d1.getFullYear()+"-"+(d1.getMonth()+1)+"-"+currentDate;
if(d1.getMonth()!=month){
oSpan.style.backgroundColor="#ccc";
}
oSpan.onclick=function(){
ele.value=this.curDate;
document.body.removeChild(oCalendar);
oCalendar=null }
d1.setDate(++currentDate);
oDateContent.appendChild(oSpan);
}
}
} function offset(ele){
var l=ele.offsetLeft;
var t=ele.offsetTop;
var p=ele.offsetParent;
while(p){
if(window.navigator.userAgent.indexOf("MSIE 8")>-1){
l+=p.offsetLeft;
t+=p.offsetTop;
}else{
l+=p.offsetLeft+p.clientLeft;
t+=p.offsetTop+p.clientTop;
}
p=p.offsetParent;
}
return {left:l,top:t}
} </script>