日期:2014-05-16 浏览次数:20328 次
var $id = function (id) { return "string" == typeof id ? document.getElementById(id) : id; }; var Class = { create: function() { return function() { this.initialize.apply(this, arguments); } } } Object.extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } return destination; } var Calendar = Class.create(); Calendar.prototype = { initialize: function(container, options) { this.Container = $id(container);//table结构容器 this.Days = [];//日期列表 this.SetOptions(options); this.Year = this.options.Year; this.Month = this.options.Month; this.SelectDay = this.options.SelectDay ? new Date(this.options.SelectDay) : null; this.onSelectDay = this.options.onSelectDay; this.onToday = this.options.onToday; this.onFinish = this.options.onFinish; this.Draw(); }, SetOptions: function(options) { this.options = {//默认值 Year: new Date().getFullYear(), Month: new Date().getMonth() + 1, SelectDay: null,//选择日期 onSelectDay: function(){}, onToday: function(){}, onFinish: function(){} }; Object.extend(this.options, options || {}); }, //上月 PreMonth: function() { //取得上月日期对象 var d = new Date(this.Year, this.Month - 2, 1); //设置属性 this.Year = d.getFullYear(); this.Month = d.getMonth() + 1; //重绘日历 this.Draw(); }, //下月 NextMonth: function() { var d = new Date(this.Year, this.Month, 1); this.Year = d.getFullYear(); this.Month = d.getMonth() + 1; this.Draw(); }, //上年 PreYear: function() { //取得上月日期对象 var d = new Date(this.Year, this.Month - 2, 1); //设置属性 this.Year = d.getFullYear()-1; //重绘日历 this.Draw(); }, //下年 NextYear: function() { var d = new Date(this.Year, this.Month, 1); this.Year = d.getFullYear()+1; this.Draw(); }, Draw: function() { //保存日期列表 var arr = []; //用当月第一天在一周中的日期值作为当月离第一天的天数 for(var i = 1, firstDay = new Date(this.Year, this.Month - 1, 1).getDay(); i <= firstDay; i++){ arr.push(" "); } //用当月最后一天在一个月中的日期值作为当月的天数 for(var i = 1, monthDay = new Date(this.Year, this.Month, 0).getDate(); i <= monthDay; i++){ arr.push(i); } //插入日期 var frag = document.createDocumentFragment(); this.Days = []; while(arr.length > 0){ //每个星期插入一个tr var row = document.createElement("tr"); //星期 for(var i = 1; i <= 7; i++){ var cell = document.createElement("td"); var a = document.createElement("a"); var alink = cell.appendChild(a); alink.innerHTML = " "; if(arr.length > 0){ var d = arr.shift(); alink.innerHTML = d; if(d > 0){ this.Days[d] = alink; var on = new Date(this.Year, this.Month - 1, d); //判断是否今日