js日历功能对象


Posted in Javascript onJanuary 12, 2012

需求产生问题:本来想在网上找个js代码,可是发现要么太复杂,要么不好控制,要么兼容性不好......
问题分析: 发现不是优秀的就是最好的.... 适合自己的就是最好的。
问题解决: 决定自己写一个日历 功能。
代码呈现:
日历主程序

var calendar={ 
STR:function(){with(this.data)return ""+Y+"年,"+M+"月,"+D+"号,星期"+WN[W];}, 
V:function(o,spli,sx){with(this)return o[sx]=""+data.Y+spli+data.M+spli+data.D}, 
T:function(){with(this)return data.TABLE}, 
dnY:function(){with(this){calendarChange(data.Y+1,data.M-1,data.D)}}, 
dnM:function(){with(this){calendarChange(data.Y,data.M,data.D)}}, 
upY:function(){with(this){calendarChange(data.Y-1,data.M-1,data.D)}}, 
upM:function(){with(this){calendarChange(data.Y,data.M-2,data.D)}}, 
day:function(o){with(this){data.D=o;calendarChange(data.Y,data.M-1,data.D)}}, 
data:{Y:null,M:null,D:null,W:null,H:null,U:null,YMD:null,YMD_C:"hot",ARR:null,TABLE:null,MN:null,WN:null,SELECT:null,TADAY:new Date(),TADAY_C:"taday",ROWLEN:7,VALUE:null}, 
creatTable:function(){with(this){ 
var table="<table height=100% width=100%><tr>" 
for(var i=0;i<data.ROWLEN;i++){var t=data.WN[i]||" ";table+="<th>"+t+"</th>";} 
for(var i in data.ARR){var showText=data.ARR[i]||" ",br=i%data.ROWLEN,title,css; 
if(!br){table+="</tr><tr>"}; 
data.ARR[i]?title=data.Y+"-"+data.M+"-"+showText:title=""; 
String(data.TADAY.getDate())==String(data.ARR[i])? 
(data.YMD.getFullYear()==data.TADAY.getFullYear()&&data.YMD.getMonth()==data.TADAY.getMonth()&&data.YMD.getDate()==data.TADAY.getDate())? 
css=data.TADAY_C+" "+data.YMD_C:css=data.YMD_C:css=""; 
table+="<td title='"+title+"' class="+css+">"+showText+"</td>"; 
}table+="</tr></table>" 
data.TABLE=table;return table; 
}}, 
calendarStarArr:function(userY,userM,userD){with(this){ 
var Arr=[]; 
var now = new Date(userY,userM,userD); 
var LastDay = now.getLastDay(); 
var FirstDayofWeek = now.FirstDayofWeek(); 
data.YMD=now;data.Y=now.getFullYear(); 
data.M=now.getMonth()+1;data.D=now.getDate();data.W=now.getDay(); 
while(Arr.length!=FirstDayofWeek){Arr.push(false)} 
for(var i=0;i<LastDay;i++){Arr.push(i+1)} 
while(Arr.length%data.ROWLEN!=0){Arr.push(false)} 
data.ARR=Arr;return Arr; 
}}, 
calendarChange:function(userY,userM,userD){with(this){ 
calendarStarArr(userY,userM,userD);creatTable() 
}}, 
calendarStar:function(userY,userM,userD){with(this){ 
data.MN = ["零","一","二","三","四","五","六","七","八","九","十","十一","十二"]; 
data.WN = ["日","一","二","三","四","五","六"]; 
calendarChange(userY,userM,userD); 
}}, 
init:function(){with(this){ 
Date.prototype.getLastDay=function(){return(new Date(this.getFullYear(),this.getMonth()+1,0).getDate())} 
Date.prototype.FirstDayofWeek=function(){return(new Date(this.getFullYear(),this.getMonth(),1).getDay())} 
calendarStar(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()) 
}} 
}

代码应用讲解:
var aa=new calendar()//创建一个新日历 
aa.init()//日历初始

obj.innerHTML=aa.STR()//显示日期字符串 
obj.innerHTML=aa.T()//显示表格 
aa.dnY()//下一年 
aa.upY()//上一年 
aa.dnM()//下一月 
aa.upM()//上一月 
aa.day(Number)//改变日历显示的几号(Number 需要传一个数值型参数)

这个日历对象实现的是日历的核心功能功能,
具体是日历放在哪里,
哪个事件触发哪个函数这个我没有写,
要是那样的话就失去我写这个日历的原有目的了。
怎么大家都没反应,我弄个例子,大家看下吧
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>js日历效果-wangzf</title> 
</head> 
<style> 
html,body{height:100%;width:100%;} 
*{margin:0;padding:0;} 
.calendarBox{width:100%;} 
#calendar_control{background:#a3a3b4;} 
#calendar_control input{min-width:inherit;} 
#calendarBox{width:100%;height:100%;} 
#calendar_str{cursor:pointer;text-align:center;font-weight:lighter;} 
#calendar_body td{background:#f0f0f0;cursor:pointer;} 
#calendar_body td.taday{background:#0CF;} 
#calendar_body td.hot{background:#FF6;} 
#calendar_body th{background:#3f526f;color:#fff;} 
#calendar_body td,#calendar_body th{text-align:center;} 
</style> 
<script> 
var calendar={ 
STR:function(){with(this.data)return ""+Y+"年,"+M+"月,"+D+"号,星期"+WN[W];}, 
V:function(spli){with(this)return ""+data.Y+spli+data.M+spli+data.D}, 
T:function(){with(this)return data.TABLE}, 
dnY:function(){with(this){calendarChange(data.Y+1,data.M-1,data.D)}}, 
dnM:function(){with(this){calendarChange(data.Y,data.M,data.D)}}, 
upY:function(){with(this){calendarChange(data.Y-1,data.M-1,data.D)}}, 
upM:function(){with(this){calendarChange(data.Y,data.M-2,data.D)}}, 
day:function(o){with(this){data.D=o;calendarChange(data.Y,data.M-1,data.D)}}, 
data:{Y:null,M:null,D:null,W:null,H:null,U:null,YMD:null,YMD_C:"hot",ARR:null,TABLE:null,MN:null,WN:null,SELECT:null,TADAY:new Date(),TADAY_C:"taday",ROWLEN:7,VALUE:null}, 
creatTable:function(){with(this){ 
var table="<table height=100% width=100%><tr>" 
for(var i=0;i<data.ROWLEN;i++){var t=data.WN[i]||" ";table+="<th>"+t+"</th>";} 
for(var i in data.ARR){var showText=data.ARR[i]||" ",br=i%data.ROWLEN,title,css=""; 
if(!br){table+="</tr><tr>"}; 
data.ARR[i]?title=data.Y+"-"+data.M+"-"+showText:title=""; 
if(String(data.D)==String(data.ARR[i])){css+=" "+data.YMD_C;} 
if(data.YMD.getFullYear()==data.TADAY.getFullYear()&&data.YMD.getMonth()==data.TADAY.getMonth()&&String(data.TADAY.getDate())==String(data.ARR[i])){css=" "+data.TADAY_C} 
table+="<td title='"+title+"' class="+css+">"+showText+"</td>"; 
}table+="</tr></table>" 
data.TABLE=table;return table; 
}}, 
calendarStarArr:function(userY,userM,userD){with(this){ 
var Arr=[]; 
var now = new Date(userY,userM,userD); 
var LastDay = now.getLastDay(); 
var FirstDayofWeek = now.FirstDayofWeek(); 
data.YMD=now;data.Y=now.getFullYear(); 
data.M=now.getMonth()+1;data.D=now.getDate();data.W=now.getDay(); 
while(Arr.length!=FirstDayofWeek){Arr.push(false)} 
for(var i=0;i<LastDay;i++){Arr.push(i+1)} 
while(Arr.length%data.ROWLEN!=0){Arr.push(false)} 
data.ARR=Arr;return Arr; 
}}, 
calendarChange:function(userY,userM,userD){with(this){ 
calendarStarArr(userY,userM,userD);creatTable() 
}}, 
calendarStar:function(userY,userM,userD){with(this){ 
data.MN = ["零","一","二","三","四","五","六","七","八","九","十","十一","十二"]; 
data.WN = ["日","一","二","三","四","五","六"]; 
calendarChange(userY,userM,userD); 
}}, 
init:function(){with(this){ 
Date.prototype.getLastDay=function(){return(new Date(this.getFullYear(),this.getMonth()+1,0).getDate())} 
Date.prototype.FirstDayofWeek=function(){return(new Date(this.getFullYear(),this.getMonth(),1).getDay())} 
calendarStar(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()) 
}} 
} 
</script> 
<body id="body"> 
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0"> 
<tr height="10%"> 
<td><table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0" id="calendar_control"> 
<tr> 
<th width="20%" align="left" scope="col"> 
<input type="button" title="上一年" value="<<" id="calendar_upY" /> 
<input type="button" title="上一月" value="<" id="calendar_upM" /></th> 
<th width="56%" id="calendar_str" scope="col" title="点击返回今天."></th> 
<th width="24%" align="right" scope="col"> 
<input type="button" title="下一年" value=">>" id="calendar_dnY" /> 
<input type="button" title="下一月" value=">" id="calendar_dnM" /></th> 
</tr> 
</table></td> 
</tr> 
<tr> 
<td height="90%" id="calendar_body"></td> 
</tr> 
</table> 
<script> 
calendar.init() 
function calendarChange(){ 
var calendar_body=document.getElementById("calendar_body") 
calendar_body.innerHTML=calendar.T()//显示表格 
var calendar_str=document.getElementById("calendar_str") 
calendar_str.innerHTML=calendar.STR()//显示日期字符串 
calendarControl()//调用日历控制 
} 
calendarChange()//日历更新 
function calendarControl(){ 
var calendar_str=document.getElementById("calendar_str") 
calendar_str.onclick=function(){ calendar.init();calendarChange()}//返回今天功能实现 
var calendar_dnY=document.getElementById("calendar_dnY") 
calendar_dnY.onclick=function(){ calendar.dnY();calendarChange()}//下一年 功能实现 
var calendar_dnM=document.getElementById("calendar_dnM") 
calendar_dnM.onclick=function(){ calendar.dnM();calendarChange()}//下一月 功能实现 
var calendar_upY=document.getElementById("calendar_upY") 
calendar_upY.onclick=function(){ calendar.upY();calendarChange()}//上一年 功能实现 
var calendar_upM=document.getElementById("calendar_upM") 
calendar_upM.onclick=function(){ calendar.upM();calendarChange()}//上一月 功能实现 
var calendar_day=document.getElementById("calendar_body").getElementsByTagName("td"); 
for(var i in calendar_day){calendar_day[i].onclick=function(){var N=Number(this.innerText);if(N){calendar.day(N);calendarChange()}}}//当天日期 功能实现 
} 
</script> 
</body> 
</html>
Javascript 相关文章推荐
URL地址中的#符号使用说明
Feb 12 Javascript
javascript的字符串按引用复制和传递,按值来比较介绍与应用
Dec 28 Javascript
JQuery中form验证出错信息的查看方法
Oct 08 Javascript
js实现精美的银灰色竖排折叠菜单
May 16 Javascript
在JavaScript中处理字符串之link()方法的使用
Jun 08 Javascript
基于jquery实现鼠标左右拖动滑块滑动附源码下载
Dec 23 Javascript
ionic在开发ios系统微信时键盘挡住输入框的解决方法(键盘弹出问题)
Sep 06 Javascript
javascript使用 concat 方法对数组进行合并的方法
Sep 08 Javascript
基于Vue2的独立构建与运行时构建的差别(详解)
Dec 06 Javascript
解决微信小程序中的滚动穿透问题
Sep 16 Javascript
简单了解常用的JavaScript 库
Jul 16 Javascript
JS如何实现基于websocket的多端桥接平台
May 14 Javascript
关于 文本框默认值 的操作js代码
Jan 12 #Javascript
新发现一个骗链接的方法(js读取cookies)
Jan 11 #Javascript
JS读取cookies信息(记录用户名)
Jan 10 #Javascript
判断对象是否Window的实现代码
Jan 10 #Javascript
jQuery在IE下使用未闭合的xml代码创建元素时的Bug介绍
Jan 10 #Javascript
javascript中onmouse事件在div中失效问题的解决方法
Jan 09 #Javascript
jQuery 阴影插件代码分享
Jan 09 #Javascript
You might like
通过curl模拟post和get方式提交的表单类
2014/04/23 PHP
php禁用cookie后session设置方法分析
2016/10/19 PHP
插件:检测javascript的内存泄漏
2007/03/04 Javascript
JS实现兼容各浏览器解析XML文档数据的方法
2015/06/01 Javascript
Vue 短信验证码组件开发详解
2017/02/14 Javascript
微信小程序中form 表单提交和取值实例详解
2017/04/20 Javascript
Angular2 组件通信的实例代码
2017/06/23 Javascript
Node.js五大应用性能技巧小结(必须收藏)
2017/08/09 Javascript
vue列表单项展开收缩功能之this.$refs的详解
2019/05/05 Javascript
layui问题之渲染数据表格时,仅出现10条数据的解决方法
2019/09/12 Javascript
JavaScript ECMA-262-3 深入解析(一):执行上下文实例分析
2020/04/25 Javascript
Vue通过阿里云oss的url连接直接下载文件并修改文件名的方法
2020/12/25 Vue.js
[01:03:42]VP vs VGJ.S 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
[43:58]DOTA2-DPC中国联赛定级赛 LBZS vs SAG BO3第一场 1月8日
2021/03/11 DOTA
python语言中with as的用法使用详解
2018/02/23 Python
为什么从Python 3.6开始字典有序并效率更高
2019/07/15 Python
Python制作微信好友背景墙教程(附完整代码)
2019/07/17 Python
python 设置xlabel,ylabel 坐标轴字体大小,字体类型
2019/07/23 Python
Python 硬币兑换问题
2019/07/29 Python
python将时分秒转换成秒的实例
2019/12/07 Python
python的json包位置及用法总结
2020/06/21 Python
Python代码需要缩进吗
2020/07/01 Python
Python使用xlrd实现读取合并单元格
2020/07/09 Python
python设置表格边框的具体方法
2020/07/17 Python
CSS3教程(5):网页背景图片
2009/04/02 HTML / CSS
成人毕业生自我鉴定
2013/10/18 职场文书
机电工程学生自荐信范文
2013/12/07 职场文书
学校后勤人员职责
2013/12/27 职场文书
2014自荐信的写作技巧
2014/01/28 职场文书
村党支部书记承诺书
2014/05/29 职场文书
义和团口号
2014/06/17 职场文书
学校领导班子四风问题整改意见
2014/10/02 职场文书
领导班子群众路线与四风问题对照检查材料思想汇报
2014/10/11 职场文书
党政领导班子群众路线对照检查材料
2014/10/26 职场文书
MATLAB 如何求取离散点的曲率最大值
2021/04/16 Python
win11怎么用快捷键锁屏? windows11锁屏的几种方法
2021/11/21 数码科技