[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行] 以下是源码: 复制代码 代码如下: var DatePicker = function () { var $ = function (i) {return document.getElementById(i)}, addEvent = function (o, e, f) {o.addEventListener ? o.addEventListener(e, f, false) : o.attachEvent('on'+e, function(){f.call(o)})}, getPos = function (el) { for (var pos = {x:0, y:0}; el; el = el.offsetParent) { pos.x += el.offsetLeft; pos.y += el.offsetTop; } return pos; }
var init = function (n, config) { window[n] = this; Date.prototype._fd = function () {var d = new Date(this); d.setDate(1); return d.getDay()}; Date.prototype._fc = function () {var d1 = new Date(this), d2 = new Date(this); d1.setDate(1); d2.setDate(1); d2.setMonth(d2.getMonth()+1); return (d2-d1)/86400000;}; this.n = n; this.config = config; this.D = new Date; this.el = $(config.inputId); this.el.title = this.n+'DatePicker';
this.update(); this.bind(); } init.prototype = {
update : function (y, m) { var con = [], week = ['Su','Mo','Tu','We','Th','Fr','Sa'], D = this.D, _this = this; fn = function (a, b) {return '
'+b+'
'}, _html = '
'; y && D.setYear(D.getFullYear() + y); m && D.setMonth(D.getMonth() + m); var year = D.getFullYear(), month = D.getMonth() + 1, date = D.getDate(); for (var i=0; i<week.length; i++) con.push('
'+week[i]+'
'); for (var i=0; i<D._fd(); i++ ) con.push('
'); for (var i=0; i<D._fc(); i++ ) con.push('
'+(i+1)+'
'); var toend = con.length%7; if (toend != 0) for (var i=0; i<7-toend; i++) con.push('