注释掉的是默认的css样式,你可以修改成自己的样式实现另一个风格,大家参考使用吧
复制代码 代码如下:
/**
* jquery calendar plugin
*/
(function($, window) { 'use strict';
$.fn.calendar = function(options) {
//check is select, if nothing select, return this
if (!this.length) {
if (options && options.debug && window.console) {
console.log(nothing select);
}
return this;
}
var self = $(this);
// default parameter setting
var defaults = {
csspath: '', //user-define loading path of css file
eventname: 'click', //user-define the event name that triggers the control
onselectdate: null, //callback function after select date
autoclose: false
};
//inherit user-defined parameter
defaults = $.extend(defaults, options);
//default as data of the day
var d_date = new date();
var _date = {
year: d_date.getfullyear(),
month: d_date.getmonth() + 1,
day: d_date.getdate(),
week: d_date.getday()
};
//default template of plugin
var calendardiv = '
';
calendardiv += '';
calendardiv += '';
calendardiv += '';
calendardiv += '>
';
calendardiv += '>>
';
calendardiv += '-
'
calendardiv += '';
calendardiv += '日
';
calendardiv += '一
';
calendardiv += '二
';
calendardiv += '三
';
calendardiv += '四
';
calendardiv += '五
';
calendardiv += '六
';
calendardiv += '
';
calendardiv += '';
for (var k = 0; k calendardiv += '' + '' + '
';
}
calendardiv += '
'; var calendaraction = {
//initialization
initaction: function() {
calendaraction.thisclick();
calendaraction.inputchange();
calendaraction.buttonchange();
calendaraction.choosedate();
},
//click to display
thisclick: function() {
self.bind(defaults.eventname, function(e) {
calendaraction.showcalendar();
});
},
//when year and month in the input box changes
inputchange: function() {
$('#calendar_year, #calendar_month').bind('change', function() {
var year = $('#calendar_year').val(),
month = $('#calendar_month').val();
if (!/^\d{4}$/.test(year)) {
alert('please input four-digit year');
return false;
}
if (!/^\d{1,2}$/.test(month) || (month > 12 || month alert('please input proper month');
return false;
}
//initialize after year and month changes
init_day_numbers(year, month);
});
},
//click to change year and month
buttonchange: function() {
$('#last-year').bind('click', function() {
var lastyear = parseint($('#calendar_year').val()) - 1 > 0 ? parseint($('#calendar_year').val()) - 1 : 1;
$('#calendar_year').val(lastyear);
init_day_numbers(lastyear, $('#calendar_month').val());
});
$('#last-month').bind('click', function() {
var lastmonth = parseint($('#calendar_month').val()) - 1 > 0 ? parseint($('#calendar_month').val()) - 1 : 12,
thisyear = lastmonth == 12 ? parseint($('#calendar_year').val()) - 1 : $('#calendar_year').val();
$('#calendar_month').val(lastmonth);
$('#calendar_year').val(thisyear);
init_day_numbers(thisyear, lastmonth);
});
$('#next-year').bind('click', function() {
var nextyear = parseint($('#calendar_year').val()) + 1;
$('#calendar_year').val(nextyear);
init_day_numbers(nextyear, $('#calendar_month').val());
});
$('#next-month').bind('click', function() {
var nextmonth = parseint($('#calendar_month').val()) + 1 thisyear = nextmonth == 1 ? parseint($('#calendar_year').val()) + 1 : $('#calendar_year').val();
$('#calendar_month').val(nextmonth);
$('#calendar_year').val(thisyear);
init_day_numbers(thisyear, nextmonth);
});
},
//set the relative offset between control and webpage, then display
showcalendar: function() {
var o_x = self.offset().left,
o_y = self.offset().top + self[0].offsetheight;
$('#calendar').css({
'position': 'absolute',
'left': o_x,
'top': o_y,
'display': 'block'
});
},
//close plugin
closecalendar: function() {
$('#calendar').hide();
},
//select date
choosedate: function() {
$('.day').live('click', function() {
var _date = $(this).attr('data-date');
//trigger callback function
if ($.isfunction(defaults.onselectdate)) {
defaults.onselectdate.call(this, _date);
}
if (defaults.autoclose) {
calendaraction.closecalendar();
}
});
}
};
//write calendar template in webpage
$('body').append(calendardiv);
//initialize the year, month, day of calendar list
init_day_numbers(_date.year, _date.month);
//initialize plugin action
calendaraction.initaction();
function init_day_numbers(year, month) {
var maxday = getmaxday(year, month),
firstweek,beforenumber,
startindex = beforenumber = firstweek = getfirstweek(year, month),
afternumber = 35 - maxday - beforenumber,
lastmonthmaxday = getmaxdaybylastmonth(year, month),
nextmonthmaxday = getmaxdaybynextmonth(year, month),
edge = getlastandnextdate(year, month);
var default_html = '';
for (var k = 0; k default_html += '
' + '' + '
';
};
$('.days').html(default_html);
for (var s = 1; s $('.day-number').eq(startindex).text(s);
$('.day').eq(startindex).attr('data-date', year + '-' + month + '-' + s);
if (s == _date.day && year == _date.year && month == _date.month) {
$('.day').eq(startindex).addclass('today');
}
startindex++;
};
for (var k = 0; k var realday = lastmonthmaxday - beforenumber + k + 1;
$('.day-number').eq(k).text(realday);
$('.day-number').eq(k).addclass('last-month');
$('.day').eq(k).attr('data-date', edge.lasty + '-' + edge.lastm + '-' + realday);
};
for (var l = 1; l var _index = beforenumber + maxday + (l - 1);
$('.day-number').eq(_index).text(l);
$('.day-number').eq(_index).addclass('next-month');
$('.day').eq(_index).attr('data-date', edge.nexty + '-' + edge.nextm + '-' + l);
}
} function getlastandnextdate(year, month) {
var lastm = month - 1 > 0 ? month - 1 : 12,
lasty = lastm == 12 ? year - 1 : year,
nextm = month + 1 > 12 ? 1 : month + 1,
nexty = nextm == 1 ? year + 1 : year;
return {
lastm: lastm,
lasty: lasty,
nextm: nextm,
nexty: nexty
};
}
//obtain what the first day of one month is in a week
function getfirstweek(year, month) {
var date = new date(year, month - 1, 1);
return date.getday();
}
//obtain the year, month, day of today
function gettoday() {
var date = new date();
return today = [date.getfullyear(), date.getmonth() + 1, date.getdate(), date.getday()];
}
//obtain the number of days for a month
function getmaxday(year, month) {
var date = new date(year, month, 0);
return date.getdate();
}
//obtain the number of days for last month
function getmaxdaybylastmonth(year, month) {
month = month - 1 year = month == 12 ? year - 1 : year;
return getmaxday(year, month);
}
//obtain the number of days for next month
function getmaxdaybynextmonth(year, month) {
month = month + 1 > 12 ? 1 : month + 1;
year = month == 1 ? year + 1 : year;
return getmaxday(year, month);
}
//obtain the days list of a certain year and month, return array
function getdaylist(year, month) {
var _list = [],
maxday = getmaxday(year, month),
month = month - 1;
for (var i = 1; i var d = new date(year, month, i);
_list[i - 1] = d.getday();
}
return _list;
}
}
})(jquery, window);
/*
*/
