javascript实现日历控件(年月日关闭按钮)


Posted in Javascript onDecember 12, 2012

经常使用google的朋友一定对google绚丽的日历控件记忆犹新吧,那我们也来实现一个,虽然功能和效果比不上,但重要的是实现的过程.
下面是要实现的html结构:
<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 ;
下面是详细的说明过程,有兴趣的可以去看下: 
https://3water.com/article/32572.htm
以下是实现的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="关闭"; 
} 
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="关闭"; 
} 
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="关闭"; 
} 
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="关闭"; 
} 
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="关闭"; 
body.all[41].onclick=function(){ 
this.style.backgroundColor=""; 
a.parentNode.removeChild(a); 
} 
} 
}

入口参数是要绑定的html对象,这里一般是text input.
下面是调用代码:
<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 相关文章推荐
javascript 判断数组是否已包含了某个元素的函数
May 30 Javascript
Query中click(),bind(),live(),delegate()的区别
Nov 19 Javascript
js substring从右边获取指定长度字符串(示例代码)
Dec 23 Javascript
js中通过父级进行查找定位元素
Jun 15 Javascript
js实现进度条的方法
Feb 13 Javascript
JS+CSS简单树形菜单实现方法
Sep 12 Javascript
Bootstrap CSS组件之分页(pagination)和翻页(pager)
Dec 17 Javascript
TypeScript学习之强制类型的转换
Dec 27 Javascript
老生常谈jacascript DOM节点获取
Apr 17 Javascript
值得收藏的vuejs安装教程
Nov 21 Javascript
Vue2.x中利用@font-size引入字体图标报错的解决方法
Sep 28 Javascript
Vue通过配置WebSocket并实现群聊功能
Dec 31 Javascript
关于js中alert弹出窗口文本换行问题简单详细说明
Dec 11 #Javascript
用js判断页面是否加载完成实现代码
Dec 11 #Javascript
ajax页面无刷新 IE下遭遇Ajax缓存导致数据不更新的问题
Dec 11 #Javascript
IE6浏览器下resize事件被执行了多次解决方法
Dec 11 #Javascript
什么是json和jsonp,jQuery json实例详详细说明
Dec 11 #Javascript
JavaScript子窗口ModalDialog中操作父窗口对像
Dec 11 #Javascript
javascript中window.event事件用法详解
Dec 11 #Javascript
You might like
PHP 出现乱码和Sessions验证问题的解决方法!
2008/12/06 PHP
php强制文件下载而非在浏览器打开的自定义函数分享
2014/05/08 PHP
php实现无限级分类
2014/12/24 PHP
php使用环形链表解决约瑟夫问题完整示例
2018/08/07 PHP
Javascript SHA-1:Secure Hash Algorithm
2006/12/20 Javascript
Javascript 获取滚动条位置等信息的函数
2009/09/08 Javascript
jQuery EasyUI API 中文文档 - ComboBox组合框
2011/10/07 Javascript
js调用图片隐藏&amp;显示实现代码
2013/09/13 Javascript
Javascript堆排序算法详解
2014/12/03 Javascript
使用three.js 画渐变的直线
2016/06/05 Javascript
JS封装的选项卡TAB切换效果示例
2016/09/20 Javascript
mui上拉加载更多下拉刷新数据的封装过程
2017/11/03 Javascript
JavaScript中使用import 和require打包后实现原理分析
2018/03/07 Javascript
Vue脚手架的简单使用实例
2018/07/10 Javascript
js实现一个简易计算器
2020/03/30 Javascript
[02:47]DOTA2亚洲邀请赛 HR战队出场宣传片
2015/02/07 DOTA
在Python的Django框架的视图中使用Session的方法
2015/07/23 Python
python docx 中文字体设置的操作方法
2018/05/08 Python
Python模拟简单电梯调度算法示例
2018/08/20 Python
Python Web版语音合成实例详解
2019/07/16 Python
python multiprocessing多进程变量共享与加锁的实现
2019/10/02 Python
python matplotlib 画dataframe的时间序列图实例
2019/11/20 Python
python3 tcp的粘包现象和解决办法解析
2019/12/09 Python
PyQt5 closeEvent关闭事件退出提示框原理解析
2020/01/08 Python
Python Matplotlib简易教程(小白教程)
2020/07/28 Python
获取CSDN文章内容并转换为markdown文本的python
2020/09/06 Python
利用css3-animation实现逐帧动画效果
2016/03/10 HTML / CSS
html5组织内容_动力节点Java学院整理
2017/07/10 HTML / CSS
澳大利亚游乐场设备品牌:Lifespan Kids
2019/05/24 全球购物
定制别致的瑜伽垫:Sugarmat
2019/06/21 全球购物
国际花店:Pickup Flowers
2020/04/10 全球购物
春节联欢会主持词
2014/03/24 职场文书
护士自我鉴定总结
2014/03/24 职场文书
设计专业毕业生求职信
2014/06/25 职场文书
单位法人授权委托书范本
2014/10/09 职场文书
大学生考试作弊检讨书1000字
2014/10/14 职场文书