JavaScript日历实现代码

效果如下:

javascript 代码如下:


代码如下:

var Calendar = function(){
var self = this;

self.box = document.createElement("div");
self.head = document.createElement("div");
self.datePlace;
self.body = document.createElement("div");
self.selectDay;
self.foot = document.createElement("div");
self.timePlace;

self.dateInfo = {
"now" : new Date(),
"getDate" : function(d){
d = d || self.dateInfo["now"];
return d.getFullYear() + "-" + (d.getMonth()+1) + "-" + d.getDate();
},
"getTime" : function(d){
d = d || self.dateInfo["now"];
return d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
},
"getSelectTime" : function(d){
d = d || self.dateInfo["now"];
return d.getFullYear() + "-" + (d.getMonth()+1) + "-" + self.dateInfo.selectDay +
" " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
},
"getDaysCount" : function(d){
d = d || self.dateInfo["now"];
return (new Date(d.getFullYear(),d.getMonth()+1,0)).getDate();
},
"weekOfFirstDay" : function(d){
d = d || self.dateInfo["now"];
return (new Date(d.getFullYear(),d.getMonth(),1)).getDay();
}
};
self.dateInfo.selectDay = self.dateInfo["now"].getDate();

this.init();
};
Calendar.prototype = {
init : function(){
this.initDom();
},
initDom : function(){
var self = this;
//head
var o = {"preYear":"<<","preMonth":"<","date":self.dateInfo["getDate"](),"nextMonth":">","nextYear":">>"};
for(var i in o){
var __ = o[i], tag = document.createElement("span");
tag.innerHTML = __.toString();
i!="date" && (tag.onclick = (function(fn){
return function(){fn.call(self);}
})(self[i])
);
i=="date" && (tag.className = "dateShow",self.datePlace = tag);

self.head.appendChild(tag);
}
self.head.className = "cal-head";

//body
self.buildBody();
self.body.className = "cal-body";

//foot
self.timePlace = document.createElement("span");

var dInfo = self.dateInfo;
var Valid = function(num,max){
num = /^\d+$/.test(num) ? num : -1;
if(num<0 || num>max){
return false;
}
return true;
};

var times = {"hour":["getHours","setHours"],"minutes":["getMinutes","setMinutes"],"seconds":["getSeconds","setSeconds"]};
for(var i in times){
var t = document.createElement("span");
var tInput = document.createElement("input");
t.innerHTML = tInput.value = dInfo["now"][times[i][0]]();

tInput.style.display = "none";
t.onclick = (function(tIpt){
return function(){
this.style.display = "none";
tIpt.style.display = "inline-block";
tIpt.select();
}
})(tInput);
tInput.onblur = (function(t,setFn){
return function(){
this.style.display = "none";
if(Valid(this.value,(setFn=="setHours"?23:59))){
t.innerHTML = this.value;
dInfo["now"][setFn](parseInt(this.value));
}
t.style.display = "inline-block";
}
})(t,times[i][1]);

self.timePlace.appendChild(t);
self.timePlace.appendChild(tInput);

if(i!="seconds"){
var sp = document.createElement("span");
sp.innerHTML = ":";
self.timePlace.appendChild(sp);
}
}
self.timePlace.className = "timeShow";

var okBtn = document.createElement("span");
okBtn.innerHTML = "Ok";
okBtn.className = "okBtn";
okBtn.onclick = function(){
alert(dInfo["getSelectTime"]());
};

self.foot.appendChild(self.timePlace);
self.foot.appendChild(okBtn);
self.foot.className = "cal-foot";

//relation
this.box.appendChild(self.head);
this.box.appendChild(self.body);
this.box.appendChild(self.foot);

this.box.className = "cal-box";
document.body.appendChild(this.box);
},
buildBody : function(){
var self = this,dInfo = self.dateInfo, week = dInfo["weekOfFirstDay"](),days = dInfo["getDaysCount"](),day = dInfo["now"].getDate();
var cDay = function(inner,ev){
var tag = document.createElement("span");
tag.innerHTML = inner;

ev && (tag.onclick = function(){
self.selectDay.className = self.selectDay.className.replace(" selectDay","");
self.selectDay = this;
self.selectDay.className = self.selectDay.className + " selectDay";

self.dateInfo.selectDay = inner;
});

return tag;
};
var domPgm = document.createDocumentFragment();
//weedInfo
var weeks = ["天","一","二","三","四","五","六"];
for(var i=0;i<weeks.length;i++){
domPgm.appendChild(cDay(weeks[i]));
}

//上个月的补白
for(var i=0;i<week;i++){
domPgm.appendChild(cDay(""));
}
//本月信息
parseInt(self.dateInfo.selectDay) > days && (self.dateInfo.selectDay = days);
for(var i=1;i<=days;i++){
var tag = cDay(i,true);
self.dateInfo.selectDay == i && (self.selectDay = tag,tag.className=" selectDay");
i===day && (tag.className=tag.className+" nowDay");
domPgm.appendChild(tag);
}
//下个月的补白....

self.body.innerHTML = "";
self.body.appendChild(domPgm);

},
dateShow : function(){
this.datePlace.innerHTML = this.dateInfo["getDate"]();
},
updateUI : function(){
this.dateShow();
this.buildBody();
},
preYear : function(){
this.dateInfo["now"].setYear(this.dateInfo["now"].getFullYear()-1);
this.updateUI();
},
preMonth : function(){
var dInfo = this.dateInfo,m = dInfo["now"].getMonth();
--m;
m<0 && (m = 11,dInfo["now"].setYear(this.dateInfo["now"].getFullYear()-1));
dInfo["now"].setMonth(m);
this.updateUI();
},
nextMonth : function(){
var dInfo = this.dateInfo,m = dInfo["now"].getMonth();
++m;
m>11 && (m = 0,dInfo["now"].setYear(this.dateInfo["now"].getFullYear()+1));
dInfo["now"].setMonth(m);
this.updateUI();
},
nextYear : function(){
this.dateInfo["now"].setYear(this.dateInfo["now"].getFullYear()+1);
this.updateUI();
}
};

css代码:


代码如下:

.cal-box{
width:210px;
font-family:"Courier New", Courier, monospace;
font-size:14px;
}
.cal-box span{
display:inline-block;
text-align:center;
}

.cal-head{
background-color:#FC3;
}
.cal-head span{
width:20px;
font-weight:bold;
color:#69C;
text-decoration:underline;
cursor:pointer;
}
.cal-head .dateShow{
width:130px;
text-decoration:none;
}

.cal-foot{
background-color:#FC3;
}
.cal-foot .timeShow{
width:160px;
text-align:left;
}
.cal-foot .timeShow input{
width:24px;
height:12px;
font-size:12px;
}
.cal-foot .okBtn{
width:50px;
background-color:#CCC;
cursor:pointer;
}

.cal-body{
background-color:#9CF;
}
.cal-body span{
display:inline-block;
width:30px;
text-align:center;
cursor:pointer;
}
.cal-body .nowDay{
background-color:#F00;
}
.cal-body .selectDay{
text-decoration:underline;
}

(0)

相关推荐

  • 一个js写的日历(代码部分网摘)

    特意贴出这段代码,因为它的代码简洁和清晰,觉得不错,供大家分享. ×××××××函数定义部分 复制代码 代码如下: <script type="text/javascript"> var $ = function (id) { return "string" == typeof id ? document.getElementById(id) : id; }; var Class = { create: function() { return funct

  • JS学习之一个简易的日历控件

    这个日历控件类似于园子用的日历,如下图: 这种日历控件实现起来不难,下面简单分析下我的思路: 首先,是该控件的可配置项: 复制代码 代码如下: ... settings: { firstDayOfWeek: 1, baseClass: "calendar", curDayClass: "curDay", prevMonthCellClass: "prevMonth", nextMonthCellClass: "nextMonth&quo

  • 简约JS日历控件 实例代码

    运行结果如下: 复制代码 代码如下: <script type="text/javascript" language="javascript">function choose_date_czw(date_id,objtd){if(date_id=="choose_date_czw_close"){    document.getElementByIdx_x_x("choose_date_czw_id").style

  • js日历控件(可精确到分钟)

    .menu_iframe{position:absolute; visibility:inherit; top:0px; left:0px; width:170px; z-index:-1; filter: Alpha(Opacity=0);} .cal_table{ border:#333333 solid 1px; border-collapse:collapse; background:#ffffff; font-size:12px} .cal_table td{ border:1px #

  • 推荐一个小巧的JS日历

    脚本的核心就是DateAdd函数,点击下面的演示看看效果: 查看演示 调用方法很简单, 第一步:在页面顶部包含calenderJS.js文件: <script src="path/to/calenderJS.js"></script> 点击下载这个js文件 第二步:给你的输入框增加onfocus事件属性: <input type="text" onfocus="HS_setDate(this)"> HS_set

  • JavaScript实现简单的日历效果

    只是个简单的日历模板,各位可根据需要自行添加需要的功能.该模板更多的是提供了关于年.月.日.月初.月末.星期的获取函数.各位可根据需要自行获取.欢迎提供更简便的方式方法,互相学习提高!谢谢~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>日历</title> <style type="

  • 轻量级的原生js日历插件calendar.js使用指南

    使用说明: 需要引入插件calendar.js/calendar.min.js 须要引入calendar.css 样式表,可以自定义自己想要的皮肤 本日历插件支持cmd模块化 如下调用: 复制代码 代码如下: xvDate({ 'targetId':'date1',//时间写入对象的id 'triggerId':['date1','dateBtn1'],//触发事件的对象id 'alignId':'datesWrap1',//日历对齐对象 'format':'-',//时间格式 默认'YYYY-

  • JS日历 推荐

    两年前写过一个日历,可是兼容性不好. 这次重新写了一次.  兼容多种浏览器  了解了不少东东,特别是对于W3C标准化.  如 FF和IE 对box模型的理解不同  box.style{width:100;border 1px;}  ie理解 为 box.width = 100  ff 理解 为 box.width = 100 + 1*2 = 102  可以使用这种方法使两种浏览器都可以正常浏览  box.style{width:100!important; width /**/:120px;bo

  • php+javascript的日历控件

    复制代码 代码如下: <html> <head> <title>js calendar</title> <script language="javascript"> /* Copyright Mihai Bazon, 2002-2005 | www.bazon.net/mishoo * ----------------------------------------------------------- * * The DHT

  • 纯js简单日历实现代码

    复制代码 代码如下: <!doctype html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> body,ul,li,h2,p { margin:0px; pa

随机推荐