javascript實現(xiàn)日歷控件(年月日關(guān)閉按鈕)
更新時間:2012年12月12日 09:57:34 作者:
經(jīng)常使用google的朋友一定對google絢麗的日歷控件記憶猶新吧,那我們也來實現(xiàn)一個,雖然功能和效果比不上,但重要的是實現(xiàn)的過程
經(jīng)常使用google的朋友一定對google絢麗的日歷控件記憶猶新吧,那我們也來實現(xiàn)一個,雖然功能和效果比不上,但重要的是實現(xiàn)的過程.
下面是要實現(xiàn)的html結(jié)構(gòu):
<div id="a"><div id="head"><span id="yface">年:<select id="year"></select></span><span id="mface">月:<select id="month"></select></span></div><div id="biaoti"></div><div id="body"></div></div>
先說一下日歷查詢的算法:
w=y+parseInt(y/4)+parseInt(c/4)-2*c+parseInt(26*(m+1)/10)+d-1 ;
下面是詳細(xì)的說明過程,有興趣的可以去看下:
http://www.dbjr.com.cn/article/32572.htm
以下是實現(xiàn)的javascript代碼:
sx.activex.calender={
bind:function(target){
var a=document.createElement("div");
var head=document.createElement("div");
var biaoti=document.createElement("div");
var select=document.createElement("select");
var yface=document.createElement("span");
var mface=document.createElement("span");
var body=document.createElement("div");
var select1=document.createElement("select");
yface.appendChild(select);
mface.appendChild(select1);
head.appendChild(yface);
head.appendChild(mface);
a.appendChild(head);
a.appendChild(biaoti);
a.appendChild(body);
yface.insertBefore(document.createTextNode("年 "),yface.firstChild)
mface.insertBefore(document.createTextNode("月 "),mface.firstChild)
a.style.position="absolute";
biaoti.style.height="10%";
for(var i=0;i<7;i++){
var can=document.createElement("span")
can.style.width="14%";
can.style.height="100%";
can.style.textAlign="center";
biaoti.appendChild(can);
}
biaoti.all[0].innerText="日"
biaoti.all[1].innerText="一"
biaoti.all[2].innerText="二"
biaoti.all[3].innerText="三"
biaoti.all[4].innerText="四"
biaoti.all[5].innerText="五"
biaoti.all[6].innerText="六"
head.style.height="20%";
a.style.position="absolute";
a.style.height="200px";
a.style.width="302px";
a.style.border="1px red solid";
yface.style.width="50%";
yface.style.padding="5px";
yface.style.height="100%";
select.style.width="80%";
for(var i=1960;i<2010;i++){
var option=document.createElement("option");
option.text=i;
select.add(option);
}
mface.style.width="50%";
mface.style.padding="5px";
mface.style.height="100%";
select1.style.width="80%";
for(var i=1;i<=12;i++){
var option=document.createElement("option");
option.text=i;
select1.add(option);
}
body.style.height="70%";
for(var i=0;i<42;i++){
var span=document.createElement("span");
span.style.width="14%";
span.style.height="16%";
span.style.textAlign="center";
span.onmouseover=function(){
this.style.cursor="hand";
this.tempcolor=this.style.backgroundColor;
this.style.backgroundColor="lightblue";
}
span.onmouseout=function(){
this.style.backgroundColor=this.tempcolor;
}
span.onclick=function(){
target.value=select.options[select.selectedIndex].text+"年"+select1.options[select1.selectedIndex].text+"月"+this.innerText+"日";
a.parentNode.removeChild(a);
}
body.appendChild(span);
}
select.onchange=function(){
for(var o in body.all){
body.all[o].innerText="";
if(o.toString()!="length")
body.all[o].style.backgroundColor="";
}
var year1=this.options[this.selectedIndex].text;
var month1=select1.options[select1.selectedIndex].text;
var y=parseInt(year1.substr(2,2)-0);
var c=parseInt(year1.substr(0,2));;
var m=parseInt(month1);;
m=m>=3?m:(y=y-1,m+12);
var d=1;
var w=y+parseInt(y/4)+parseInt(c/4)-2*c+parseInt(26*(m+1)/10)+d-1 ;
if(w<0) w=w+700;
w=w%7;
switch(parseInt(month1)){
case 2:
if(parseInt(year1)%4==0)
var r=29;
else
var r=28;
var day=w;
for(var d=1;d<=r;d++){
body.all[day++].innerText=d;
if(parseInt(year1)==(new Date()).getYear() && parseInt(month1)==(new Date()).getMonth()+1 && d==(new Date()).getDate())
body.all[day-1].style.backgroundColor="red";
body.all[41].innerText="關(guān)閉";
}
break;
default:
if(parseInt(month1)==1 || parseInt(month1)==3 || parseInt(month1)==5 || parseInt(month1)==7 || parseInt(month1)==8 || parseInt(month1)==10 || parseInt(month1)==12)
var r=31;
else
var r=30;
var day=w;
for(var d=1;d<=r;d++){
body.all[day++].innerText=d;
if(parseInt(year1)==(new Date()).getYear() && parseInt(month1)==(new Date()).getMonth()+1 && d==(new Date()).getDate())
body.all[day-1].style.backgroundColor="red";
body.all[41].innerText="關(guān)閉";
}
break;
}
}
select1.onchange=function(){
for(var o in body.all){
body.all[o].innerText="";
if(o.toString()!="length")
body.all[o].style.backgroundColor="";
}
var month1=this.options[this.selectedIndex].text;
var year1=select.options[select.selectedIndex].text;
var y=parseInt(year1.substr(2,2)-0);
var c=parseInt(year1.substr(0,2));
var m=parseInt(month1);
m=m>=3?m:(y=y-1,m+12);
var d=1;
var w=y+parseInt(y/4)+parseInt(c/4)-2*c+parseInt(26*(m+1)/10)+d-1 ;
if(w<0) w=w+700;
w=w%7;
switch(parseInt(month1)){
case 2:
if(parseInt(year1)%4==0)
var r=29;
else
var r=28;
var day=w;
for(var d=1;d<=r;d++){
body.all[day++].innerText=d;
if(parseInt(year1)==(new Date()).getYear() && parseInt(month1)==(new Date()).getMonth()+1 && d==(new Date()).getDate())
body.all[day-1].style.backgroundColor="red";
body.all[41].innerText="關(guān)閉";
}
break;
default:
if(parseInt(month1)==1 || parseInt(month1)==3 || parseInt(month1)==5 || parseInt(month1)==7 || parseInt(month1)==8 || parseInt(month1)==10 || parseInt(month1)==12)
var r=31;
else
var r=30;
var day=w;
for(var d=1;d<=r;d++){
body.all[day++].innerText=d;
if(parseInt(year1)==(new Date()).getYear() && parseInt(month1)==(new Date()).getMonth()+1 && d==(new Date()).getDate())
body.all[day-1].style.backgroundColor="red";
body.all[41].innerText="關(guān)閉";
}
break;
}
}
var date=new Date();
for(var s1=0;s1<select.options.length;s1++){
if(parseInt(select.options[s1].text)==parseInt(date.getYear())){
select.options[s1].selected=true;
break;
}
}
for(var s2=0;s2<select1.options.length;s2++){
if(parseInt(select1.options[s2].text)==parseInt(date.getMonth())+1){
select1.options[s2].selected=true;
break;
}
}
select.onchange();
for(var i in body.all){
if(body.all[i].innerText==date.getDate()){
body.all[i].style.backgroundColor="red";
}
}
target.onfocus=function(){
document.body.appendChild(a);
a.style.left=target.offsetLeft+"px";;
a.style.top=target.offsetTop+target.offsetHeight+"px";
}
target.onblur=function(){
if(a && window.event.clientY>a.offsetTop && window.event.clientY<a.offsetTop+a.offsetHeight && window.event.clientX>a.offsetLeft && window.event.clientX<a.offsetLeft+a.offsetWidth)
return;
if(!a) return;
a.parentNode.removeChild(a);
}
body.all[41].innerText="關(guān)閉";
body.all[41].onclick=function(){
this.style.backgroundColor="";
a.parentNode.removeChild(a);
}
}
}
入口參數(shù)是要綁定的html對象,這里一般是text input.
下面是調(diào)用代碼:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script src="kongjian.js"></script>
<input type="text" id="a">
<script>
sx.activex.calender.bind(document.getElementById("a"));
</script>
</body>
</html>
差不多就這樣,代碼比較冗長,不是很好,如果哪位朋友有更好的辦法,請與我多多交流啊.
下面是要實現(xiàn)的html結(jié)構(gòu):
<div id="a"><div id="head"><span id="yface">年:<select id="year"></select></span><span id="mface">月:<select id="month"></select></span></div><div id="biaoti"></div><div id="body"></div></div>
先說一下日歷查詢的算法:
w=y+parseInt(y/4)+parseInt(c/4)-2*c+parseInt(26*(m+1)/10)+d-1 ;
下面是詳細(xì)的說明過程,有興趣的可以去看下:
http://www.dbjr.com.cn/article/32572.htm
以下是實現(xiàn)的javascript代碼:
復(fù)制代碼 代碼如下:
sx.activex.calender={
bind:function(target){
var a=document.createElement("div");
var head=document.createElement("div");
var biaoti=document.createElement("div");
var select=document.createElement("select");
var yface=document.createElement("span");
var mface=document.createElement("span");
var body=document.createElement("div");
var select1=document.createElement("select");
yface.appendChild(select);
mface.appendChild(select1);
head.appendChild(yface);
head.appendChild(mface);
a.appendChild(head);
a.appendChild(biaoti);
a.appendChild(body);
yface.insertBefore(document.createTextNode("年 "),yface.firstChild)
mface.insertBefore(document.createTextNode("月 "),mface.firstChild)
a.style.position="absolute";
biaoti.style.height="10%";
for(var i=0;i<7;i++){
var can=document.createElement("span")
can.style.width="14%";
can.style.height="100%";
can.style.textAlign="center";
biaoti.appendChild(can);
}
biaoti.all[0].innerText="日"
biaoti.all[1].innerText="一"
biaoti.all[2].innerText="二"
biaoti.all[3].innerText="三"
biaoti.all[4].innerText="四"
biaoti.all[5].innerText="五"
biaoti.all[6].innerText="六"
head.style.height="20%";
a.style.position="absolute";
a.style.height="200px";
a.style.width="302px";
a.style.border="1px red solid";
yface.style.width="50%";
yface.style.padding="5px";
yface.style.height="100%";
select.style.width="80%";
for(var i=1960;i<2010;i++){
var option=document.createElement("option");
option.text=i;
select.add(option);
}
mface.style.width="50%";
mface.style.padding="5px";
mface.style.height="100%";
select1.style.width="80%";
for(var i=1;i<=12;i++){
var option=document.createElement("option");
option.text=i;
select1.add(option);
}
body.style.height="70%";
for(var i=0;i<42;i++){
var span=document.createElement("span");
span.style.width="14%";
span.style.height="16%";
span.style.textAlign="center";
span.onmouseover=function(){
this.style.cursor="hand";
this.tempcolor=this.style.backgroundColor;
this.style.backgroundColor="lightblue";
}
span.onmouseout=function(){
this.style.backgroundColor=this.tempcolor;
}
span.onclick=function(){
target.value=select.options[select.selectedIndex].text+"年"+select1.options[select1.selectedIndex].text+"月"+this.innerText+"日";
a.parentNode.removeChild(a);
}
body.appendChild(span);
}
select.onchange=function(){
for(var o in body.all){
body.all[o].innerText="";
if(o.toString()!="length")
body.all[o].style.backgroundColor="";
}
var year1=this.options[this.selectedIndex].text;
var month1=select1.options[select1.selectedIndex].text;
var y=parseInt(year1.substr(2,2)-0);
var c=parseInt(year1.substr(0,2));;
var m=parseInt(month1);;
m=m>=3?m:(y=y-1,m+12);
var d=1;
var w=y+parseInt(y/4)+parseInt(c/4)-2*c+parseInt(26*(m+1)/10)+d-1 ;
if(w<0) w=w+700;
w=w%7;
switch(parseInt(month1)){
case 2:
if(parseInt(year1)%4==0)
var r=29;
else
var r=28;
var day=w;
for(var d=1;d<=r;d++){
body.all[day++].innerText=d;
if(parseInt(year1)==(new Date()).getYear() && parseInt(month1)==(new Date()).getMonth()+1 && d==(new Date()).getDate())
body.all[day-1].style.backgroundColor="red";
body.all[41].innerText="關(guān)閉";
}
break;
default:
if(parseInt(month1)==1 || parseInt(month1)==3 || parseInt(month1)==5 || parseInt(month1)==7 || parseInt(month1)==8 || parseInt(month1)==10 || parseInt(month1)==12)
var r=31;
else
var r=30;
var day=w;
for(var d=1;d<=r;d++){
body.all[day++].innerText=d;
if(parseInt(year1)==(new Date()).getYear() && parseInt(month1)==(new Date()).getMonth()+1 && d==(new Date()).getDate())
body.all[day-1].style.backgroundColor="red";
body.all[41].innerText="關(guān)閉";
}
break;
}
}
select1.onchange=function(){
for(var o in body.all){
body.all[o].innerText="";
if(o.toString()!="length")
body.all[o].style.backgroundColor="";
}
var month1=this.options[this.selectedIndex].text;
var year1=select.options[select.selectedIndex].text;
var y=parseInt(year1.substr(2,2)-0);
var c=parseInt(year1.substr(0,2));
var m=parseInt(month1);
m=m>=3?m:(y=y-1,m+12);
var d=1;
var w=y+parseInt(y/4)+parseInt(c/4)-2*c+parseInt(26*(m+1)/10)+d-1 ;
if(w<0) w=w+700;
w=w%7;
switch(parseInt(month1)){
case 2:
if(parseInt(year1)%4==0)
var r=29;
else
var r=28;
var day=w;
for(var d=1;d<=r;d++){
body.all[day++].innerText=d;
if(parseInt(year1)==(new Date()).getYear() && parseInt(month1)==(new Date()).getMonth()+1 && d==(new Date()).getDate())
body.all[day-1].style.backgroundColor="red";
body.all[41].innerText="關(guān)閉";
}
break;
default:
if(parseInt(month1)==1 || parseInt(month1)==3 || parseInt(month1)==5 || parseInt(month1)==7 || parseInt(month1)==8 || parseInt(month1)==10 || parseInt(month1)==12)
var r=31;
else
var r=30;
var day=w;
for(var d=1;d<=r;d++){
body.all[day++].innerText=d;
if(parseInt(year1)==(new Date()).getYear() && parseInt(month1)==(new Date()).getMonth()+1 && d==(new Date()).getDate())
body.all[day-1].style.backgroundColor="red";
body.all[41].innerText="關(guān)閉";
}
break;
}
}
var date=new Date();
for(var s1=0;s1<select.options.length;s1++){
if(parseInt(select.options[s1].text)==parseInt(date.getYear())){
select.options[s1].selected=true;
break;
}
}
for(var s2=0;s2<select1.options.length;s2++){
if(parseInt(select1.options[s2].text)==parseInt(date.getMonth())+1){
select1.options[s2].selected=true;
break;
}
}
select.onchange();
for(var i in body.all){
if(body.all[i].innerText==date.getDate()){
body.all[i].style.backgroundColor="red";
}
}
target.onfocus=function(){
document.body.appendChild(a);
a.style.left=target.offsetLeft+"px";;
a.style.top=target.offsetTop+target.offsetHeight+"px";
}
target.onblur=function(){
if(a && window.event.clientY>a.offsetTop && window.event.clientY<a.offsetTop+a.offsetHeight && window.event.clientX>a.offsetLeft && window.event.clientX<a.offsetLeft+a.offsetWidth)
return;
if(!a) return;
a.parentNode.removeChild(a);
}
body.all[41].innerText="關(guān)閉";
body.all[41].onclick=function(){
this.style.backgroundColor="";
a.parentNode.removeChild(a);
}
}
}
入口參數(shù)是要綁定的html對象,這里一般是text input.
下面是調(diào)用代碼:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script src="kongjian.js"></script>
<input type="text" id="a">
<script>
sx.activex.calender.bind(document.getElementById("a"));
</script>
</body>
</html>
差不多就這樣,代碼比較冗長,不是很好,如果哪位朋友有更好的辦法,請與我多多交流啊.
您可能感興趣的文章:
- JavaScript時間操作之年月日星期級聯(lián)操作
- JavaScript顯示當(dāng)然日期和時間即年月日星期和時間
- js獲取時間精確到秒(年月日)
- 時間戳轉(zhuǎn)換為時間 年月日時間的JS函數(shù)
- json格式的時間顯示為正常年月日的方法
- 用js實現(xiàn)每隔一秒刷新時間的實例(含年月日時分秒)
- js獲取當(dāng)前年月日-YYYYmmDD格式的實現(xiàn)代碼
- 純JS實現(xiàn)出生日期[年月日]下拉菜單效果
- js顯示當(dāng)前日期時間和星期幾
- JS簡單獲取當(dāng)前日期時間的方法(如:2017-03-29 11:41:10 星期四)
- js獲取指定日期周數(shù)以及星期幾的小例子
- JS實現(xiàn)處理時間,年月日,星期的公共方法示例
相關(guān)文章
JavaScript實現(xiàn)AOP詳解(面向切面編程,裝飾者模式)
下面小編就為大家分享一篇JavaScript實現(xiàn)AOP的方法(面向切面編程,裝飾者模式),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12Layui數(shù)據(jù)表格跳轉(zhuǎn)到指定頁的實現(xiàn)方法
今天小編就為大家分享一篇Layui數(shù)據(jù)表格跳轉(zhuǎn)到指定頁的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09JavaScript實現(xiàn)DIV層拖動及動態(tài)增加新層的方法
這篇文章主要介紹了JavaScript實現(xiàn)DIV層拖動及動態(tài)增加新層的方法,設(shè)計javascript操作div層的拖動與增加的相關(guān)技巧,需要的朋友可以參考下2015-05-05JavaScript實現(xiàn)基礎(chǔ)排序算法的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實現(xiàn)基礎(chǔ)排序算法,如:冒泡排序、選擇排序、插入排序和快速排序,感興趣的可以了解一下2022-06-06