javascript写的日历类_javascript技巧_脚本之家

先看看效果: 复制代码 代码如下:

关于寒羽风的WebCalendar.js不能选当天之后的时间
有谁用过这个日历控件的,这个控件不能选择当日之后的日期,只能选择之前的日子,看了半天看不懂,我贴上代码 <!–
var cal;
var isFocus=false; //是否为焦点
//以上为 寒羽枫 2006-06-25 添加的变量

使用很简单; 先创建一个Calendar对象 复制代码 代码如下: var calendar=new
Calendar方法即可显示 复制代码 代码如下:
calendar.show() 其实调用的是pj库的show方法
因此在使用之前要先把pj库引进来,在把Calendar.js引进来就可以
Calendar的主要方法有 getDateString format是日期格式,默认是yyyy-mm-dd
show:function 显示日历 hide:function隐藏日历 fadeIn:function淡入日历
fadeOut:function淡出日历
locateAt:function把日历定位到指定的元素正下方,offsetX、offsetY可选,是相对于X、Y轴的偏移量
只定义了两个事件
onChange//当改变年份或者月份是的事件监听器,以当前Calendar对象为上下文执行fn函数
onSelect//当选择日期时触发的函数,以当前Calendar对象为上下文执行fn函数
还是把源代码粘贴上来吧 复制代码 代码如下:
//date:默认开始日期,不需要可以是null,yearListLength:年份下拉列表长度
function Calendar{ var currentDate=date||new Date(),_this=this;
this.container=pj.appendTo.setStyle({overflow:’hidden’,background:
‘#99CCFF’,border:’1px solid
#CCC’,fontSize:’12px’,height:’160px’,width:’180px’,position:”absolute”,display:’none’});
this.container.get().innerHTML=’

//选择日期 → 由 寒羽枫 2006-06-25 添加
function SelectDate(obj,strFormat)
{
var date = new Date();
var by = date.getFullYear()-10; //最小值 → 50 年前
var ey = date.getFullYear()+20; //最大值 → 50 年后
//cal = new Calendar(by, ey,1,strFormat); //初始化英文版,0 为中文版
cal = (cal==null) ? new Calendar(by, ey, 0) : cal; //不用每次都初始化
2006-12-03 修正
cal.dateFormatStyle = strFormat;
cal.show;
}
/**//**//**//**//**//**//**//**
* 返回日期
* @param d the delimiter
* @param p the pattern of your date
2006-06-25 由 寒羽枫 修改为根据用户指定的 style 来确定;
*/
//String.prototype.toDate = function {
String.prototype.toDate = function {
/**//**//**//**//**//**//**//*
if(x == null) x = “-“;
if(p == null) p = “ymd”;
var a = this.split;
var y = parseInt(a[p.indexOf;
//remember to change this next century 😉
if(y.toString().length <= 2) y += 2000;
if y = new Date().getFullYear();
var m = parseInt(a[p.indexOf – 1;
var d = parseInt(a[p.indexOf;
if d = 1;
return new Date;
*/
var y = this.substring(style.indexOf,style.lastIndexOf;//年
var m = this.substring(style.indexOf,style.lastIndexOf;//月
var d = this.substring(style.indexOf,style.lastIndexOf;//日
if y = new Date().getFullYear();
if m = new Date().getMonth();
if d = new Date().getDate();
var dt ;
eval (“dt = new Date(‘”+ y+”‘, ‘”++”‘,'”+ d +”‘)”);
return dt;
}

年月

/**//**//**//**//**//**//**//**
* 格式化日期
* @param d the delimiter
* @param p the pattern of your date
* @author meizz
*/
Date.prototype.format = function {
var o = {
“M+” : this.getMonth() + 1, //month
“d+” : this.getDate(), //day
“h+” : this.getHours(), //hour
“m+” : this.getMinutes(), //minute
“s+” : this.getSeconds(), //second
“w+” : “天一二三四五六”.charAt(this.getDay, //week
“q+” : Math.floor((this.getMonth / 3), //quarter
“S” : this.getMilliseconds() //millisecond
}
if/.test {
style = style.replace(RegExp.$1,
(this.getFullYear.substr(4 – RegExp.$1.length));
}
for(var k in o){
if(new RegExp(“”).test{
style = style.replace(RegExp.$1,
RegExp.$1.length == 1 ? o[k] :
(“00” + o[k]).substr((“” + o[k]).length));
}
}
return style;
};

1 2 3 4 5 6 7 8 9 10 11 12

/**//**//**//**//**//**//**//**
* 日历类
* @param beginYear 1990
* @param endYear 2010
* @param lang 0 可自由扩充
* @param dateFormatStyle “yyyy-MM-dd”;
* @version 2006-04-01
* @author KimSoft (jinqinghua [at] gmail.com)
* @update
*/
function Calendar(beginYear, endYear, lang, dateFormatStyle) {
this.beginYear = 1990;
this.endYear = 2010;
this.lang = 0; //0 | 1
this.dateFormatStyle = “yyyy-MM-dd”;

if (beginYear != null && endYear != null){
this.beginYear = beginYear;
this.endYear = endYear;
}
if (lang != null){
this.lang = lang
}

if (dateFormatStyle != null){
this.dateFormatStyle = dateFormatStyle
}

this.dateControl = null;
this.panel = this.getElementById(“calendarPanel”);
this.container = this.getElementById(“ContainerPanel”);
this.form = null;

this.date = new Date();
this.year = this.date.getFullYear();
this.month = this.date.getMonth();

this.colors = {
“cur_word” : “#FFFFFF”, //当日日期文字颜色
“cur_bg” : “#00FF00”, //当日日期单元格背影色
“sel_bg” : “#FFCCCC”, //已被选择的日期单元格背影色 2006-12-03
寒羽枫添加
“sun_word” : “#FF0000”, //星期天文字颜色
“sat_word” : “#0000FF”, //星期六文字颜色
“td_word_light” : “#333333”, //单元格文字颜色
“td_word_dark” : “#CCCCCC”, //单元格文字暗色
“td_bg_out” : “#EFEFEF”, //单元格背影色
“td_bg_over” : “#FFCC00”, //单元格背影色
“tr_word” : “#FFFFFF”, //日历头文字颜色
“tr_bg” : “#666666”, //日历头背影色
“input_border” : “#CCCCCC”, //input控件的边框颜色
“input_bg” : “#EFEFEF” //input控件的背影色
}

this.draw();
this.bindYear();
this.bindMonth();
//this.changeSelect();
//this.bindData(); //2006-12-30 由民工.砖家注释
}

/**//**//**//**//**//**//**//**
* 日历类属性(语言包,可自由扩展)
*/
Calendar.language = {
“year” : [[“”], [“”]],
“months” :
[[“一月”,”二月”,”三月”,”四月”,”五月”,”六月”,”七月”,”八月”,”九月”,”十月”,”十一月”,”十二月”],
[“JAN”,”FEB”,”MAR”,”APR”,”MAY”,”JUN”,”JUL”,”AUG”,”SEP”,”OCT”,”NOV”,”DEC”]
],
“weeks” : [[“日”,”一”,”二”,”三”,”四”,”五”,”六”],
[“SUN”,”MON”,”TUR”,”WED”,”THU”,”FRI”,”SAT”]
],
“clear” : [[“清空”], [“CLS”]],
“today” : [[“今天”], [“TODAY”]],
“close” : [[“关闭”], [“CLOSE”]]
}

Calendar.prototype.draw = function() {
calendar = this;

var mvAry = [];
//mvAry[mvAry.length] = ‘ <form name=”calendarForm” style=”margin:
0px;”>’; //因 <form> 不能嵌套, 2006-12-01 由寒羽枫改用 Div
mvAry[mvAry.length] = ‘ <div name=”calendarForm” style=”margin:
0px;”>’;
mvAry[mvAry.length] = ‘ <table width=”100%” border=”0″
cellpadding=”0″ cellspacing=”1″>’;
mvAry[mvAry.length] = ‘ <tr>’;
mvAry[mvAry.length] = ‘ <th align=”left” width=”1%”><input
style=”border: 1px solid ‘ + calendar.colors[“input_border”] +
‘;background-color:’ + calendar.colors[“input_bg”] +
‘;width:16px;height:20px;” name=”prevMonth” type=”button” value=”<”
/></th>’;
mvAry[mvAry.length] = ‘ <th align=”center” width=”98%”
nowrap=”nowrap”><select name=”calendarYear”
style=”font-size:12px;”></select><select
name=”calendarMonth”
style=”font-size:12px;”></select></th>’;
mvAry[mvAry.length] = ‘ <th align=”right” width=”1%”><input
style=”border: 1px solid ‘ + calendar.colors[“input_border”] +
‘;background-color:’ + calendar.colors[“input_bg”] +
‘;width:16px;height:20px;” name=”nextMonth” type=”button” value=”>”
/></th>’;
mvAry[mvAry.length] = ‘ </tr>’;
mvAry[mvAry.length] = ‘ </table>’;
mvAry[mvAry.length] = ‘ <table width=”100%” style=”border:0px solid
#CCCCCC;background-color:#FFFFFF” border=”0″ cellpadding=”3″
cellspacing=”1″>’;
mvAry[mvAry.length] = ‘ <tr>’;
for(var i = 0; i < 7; i++) {
mvAry[mvAry.length] = ‘ <th
style=”font-weight:normal;background-color:’ +
calendar.colors[“tr_bg”] + ‘;color:’ +
calendar.colors[“tr_word”] + ‘;”>’ +
Calendar.language[“weeks”][this.lang][i] + ‘</th>’;
}
mvAry[mvAry.length] = ‘ </tr>’;
for(var i = 0; i < 6;i++){
mvAry[mvAry.length] = ‘ <tr align=”center”>’;
for(var j = 0; j < 7; j++) {
if {
mvAry[mvAry.length] = ‘ <td style=”cursor:default;color:’ +
calendar.colors[“sun_word”] + ‘;”></td>’;
} else if {
mvAry[mvAry.length] = ‘ <td style=”cursor:default;color:’ +
calendar.colors[“sat_word”] + ‘;”></td>’;
} else {
mvAry[mvAry.length] = ‘ <td
style=”cursor:default;”></td>’;
}
}
mvAry[mvAry.length] = ‘ </tr>’;
}
mvAry[mvAry.length] = ‘ <tr style=”background-color:’ +
calendar.colors[“input_bg”] + ‘;”>’;
mvAry[mvAry.length] = ‘ <th colspan=”2″><input
name=”calendarClear” type=”button” value=”‘ +
Calendar.language[“clear”][this.lang] + ‘” style=”border: 1px solid
‘ + calendar.colors[“input_border”] + ‘;background-color:’ +
calendar.colors[“input_bg”] +
‘;width:100%;height:20px;font-size:12px;”/></th>’;
mvAry[mvAry.length] = ‘ <th colspan=”3″><input
name=”calendarToday” type=”button” value=”‘ +
Calendar.language[“today”][this.lang] + ‘” style=”border: 1px solid
‘ + calendar.colors[“input_border”] + ‘;background-color:’ +
calendar.colors[“input_bg”] +
‘;width:100%;height:20px;font-size:12px;”/></th>’;
mvAry[mvAry.length] = ‘ <th colspan=”2″><input
name=”calendarClose” type=”button” value=”‘ +
Calendar.language[“close”][this.lang] + ‘” style=”border: 1px solid
‘ + calendar.colors[“input_border”] + ‘;background-color:’ +
calendar.colors[“input_bg”] +
‘;width:100%;height:20px;font-size:12px;”/></th>’;
mvAry[mvAry.length] = ‘ </tr>’;
mvAry[mvAry.length] = ‘ </table>’;
//mvAry[mvAry.length] = ‘ </from>’;
mvAry[mvAry.length] = ‘ </div>’;
this.panel.innerHTML = mvAry.join;

/**//**//**//******** 以下代码由寒羽枫 2006-12-01 添加
**********/
var obj = this.getElementById(“prevMonth”);
obj.onclick = function () {calendar.goPrevMonth;}
obj.onblur = function () {calendar.onblur();}
this.prevMonth= obj;

obj = this.getElementById(“nextMonth”);
obj.onclick = function () {calendar.goNextMonth;}
obj.onblur = function () {calendar.onblur();}
this.nextMonth= obj;

obj = this.getElementById(“calendarClear”);
obj.onclick = function () {calendar.dateControl.value =
“”;calendar.hide();}
this.calendarClear = obj;

obj = this.getElementById(“calendarClose”);
obj.onclick = function () {calendar.hide();}
this.calendarClose = obj;

obj = this.getElementById(“calendarYear”);
obj.onchange = function () {calendar.update;}
obj.onblur = function () {calendar.onblur();}
this.calendarYear = obj;

obj = this.getElementById(“calendarMonth”);
with
{
onchange = function () {calendar.update;}
onblur = function () {calendar.onblur();}
}this.calendarMonth = obj;

obj = this.getElementById(“calendarToday”);
obj.onclick = function () {
var today = new Date();
/**//*calendar.date = today;
calendar.year = today.getFullYear();
calendar.month = today.getMonth();
calendar.changeSelect();
calendar.bindData();
*/ //2006-12-30 由民工.砖家注释
calendar.dateControl.value = today.format(calendar.dateFormatStyle);
calendar.hide();
}
this.calendarToday = obj;

}

//年份下拉框绑定数据
Calendar.prototype.bindYear = function() {
//var cy = this.form.calendarYear;
var cy = this.calendarYear;//2006-12-01 由寒羽枫修改
cy.length = 0;
for (var i = this.beginYear; i <= this.endYear; i++){
cy.options[cy.length] = new Option(i +
Calendar.language[“year”][this.lang], i);
}
}

//月份下拉框绑定数据
Calendar.prototype.bindMonth = function() {
//var cm = this.form.calendarMonth;
var cm = this.calendarMonth;//2006-12-01 由寒羽枫修改
cm.length = 0;
for (var i = 0; i < 12; i++){
cm.options[cm.length] = new
Option(Calendar.language[“months”][this.lang][i], i);
}
}

//向前一月
Calendar.prototype.goPrevMonth = function{
if (this.year == this.beginYear && this.month == 0){return;}
this.month–;
if (this.month == -1) {
this.year–;
this.month = 11;
}
this.date = new Date(this.year, this.month, 1);
this.changeSelect();
this.bindData();
}

//向后一月
Calendar.prototype.goNextMonth = function{
if (this.year == this.endYear && this.month == 11){return;}
this.month++;
if (this.month == 12) {
this.year++;
this.month = 0;
}
this.date = new Date(this.year, this.month, 1);
this.changeSelect();
this.bindData();
}

//改变SELECT选中状态
Calendar.prototype.changeSelect = function() {
var cy = this.calendarYear;//2006-12-01 由寒羽枫修改
var cm = this.calendarMonth;
//2006-12-30 由民工.砖家修改,减少运算次数
cy[this.date.getFullYear()-this.beginYear].selected = true;
cm[this.date.getMonth()].selected =true;
/**//*
for (var i= 0; i < cy.length; i++){
if (cy.options[i].value == this.date.getFullYear{
cy[i].selected = true;
break;
}
}
for (var i= 0; i < cm.length; i++){
if (cm.options[i].value == this.date.getMonth{
cm[i].selected = true;
break;
}
}
*/
//2006-12-30 由民工.砖家注释
}

//更新年、月
Calendar.prototype.update = function {
this.year =
e.calendarYear.options[e.calendarYear.selectedIndex].value;//2006-12-01
由寒羽枫修改
this.month =
e.calendarMonth.options[e.calendarMonth.selectedIndex].value;
this.date = new Date(this.year, this.month, 1);
this.changeSelect();
this.bindData();
}