原生js-日曆外掛程式

來源:互聯網
上載者:User

標籤:style   color   width   os   cti   for   

<!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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.