/** @Name : jeDate v2.0 日期控件 @Author: chne guojun @Date: 2015-12-28 @QQ群:516754269 @Site:https://github.com/singod/jeDate */ (function(win) { var jeDt = {}, doc = document, Cell = "#jedatebox"; /* (tag), (#id), (.className) ,(tag > .className) ,(tag > tag) ,(#id > tag.className) , (.className tag) ,(tag, tag, #id) ,(tag#id.className) ,(span > * > b) ,(input[name=radio]) */ var QD = jeDt.query =function(){function r(c,g){g=g||document;if(!/^[\w\-_#]+$/.test(c)&&g.querySelectorAll)return m(g.querySelectorAll(c));if(-1/gi,u=/^(?:[\w\-_]+)?\.([\w\-_]+)/,t=/^(?:[\w\-_]+)?#([\w\-_]+)/,v=/^([\w\*\-_]+)/,k=[null,null,null],x=/\[([\w\-_][^=]+)=([\'\[\]\w\-_]+)\]/,y=function(){var c=+new Date,g=function(){var d=1;return function(a){var b=a[c],e=d++;return b?!1:(a[c]=e,!0)}}();return function(d){for(var a=d.length,b=[],e=-1,f=0,h;f= 0; i--) { _extend(arr[i], _result); } arr[0] = _result; return _result; }; jeDt.trim = function(str) { str = str || ""; return str.replace(/^\s|\s$/g, "").replace(/\s+/g, " "); }; jeDt.attr = function(elem, key, val) { if (typeof key === "string" && typeof val === 'undefined') { return elem.getAttribute(key); } else { elem.setAttribute(key, val); } return this; }; jeDt.stopmp = function(e) { e = e || win.event; e.stopPropagation ? e.stopPropagation() :e.cancelBubble = true; return this; }; jeDt.getCss = function(obj, name) { if (obj.currentStyle) { return obj.currentStyle[name]; } else if (window.getComputedStyle) { return document.defaultView.getComputedStyle(obj)[name]; } return null; }; //查询样式是否存在 jeDt.hasClass = function(elem, cls) { elem = elem || {}; return new RegExp("\\b" + cls + "\\b").test(elem.className); }; //添加样式 jeDt.addClass = function(elem, cls) { elem = elem || {}; jeDt.hasClass(elem, cls) || (elem.className += " " + cls); elem.className = jeDt.trim(elem.className); return this; }; //删除样式 jeDt.removeClass = function(elem, cls) { elem = elem || {}; if (jeDt.hasClass(elem, cls)) { elem.className = elem.className.replace(new RegExp("(\\s|^)" + cls + "(\\s|$)"), ""); } return this; }; //事件监听器 jeDt.on = function(obj, type, fn) { if (obj.addEventListener) { obj.addEventListener(type, fn, false); } else if (obj.attachEvent) { obj.attachEvent("on" + type, fn); } else { obj["on" + type] = fn; } }; //阻断mouseup jeDt.stopMosup = function(evt, elem) { if (evt !== "mouseup") { jeDt.on(elem, "mouseup", function(ev) { jeDt.stopmp(ev); }); } }; jeDt.html = function(elem, value) { if (typeof value != "undefined" || value !== undefined && elem.nodeType === 1) { elem.innerHTML = value; } else { return elem.innerHTML; } return this; }; jeDt.text = function(elem, value) { if (value !== undefined && elem.nodeType === 1) { document.all ? elem.innerText = value :elem.textContent = value; } else { var emText = document.all ? elem.innerText :elem.textContent; return emText; } return this; }; jeDt.val = function(elem, value) { if (value !== undefined && elem.nodeType === 1) { elem.value = value; } else { return elem.value; } return this; }; jeDt.scroll = function(type) { type = type ? "scrollLeft" :"scrollTop"; return doc.body[type] | doc.documentElement[type]; }; jeDt.winarea = function(type) { return doc.documentElement[type ? "clientWidth" :"clientHeight"]; }; //转换日期格式 jeDt.parse = function(ymd, hms, format) { ymd = ymd.concat(hms); var format = format, _this = this; return format.replace(/YYYY|MM|DD|hh|mm|ss/g, function(str, index) { ymd.index = ++ymd.index | 0; return jeDt.digit(ymd[ymd.index]); }); }; //初始化日期 jeDt.nowDate = function(timestamp, format) { var De = new Date(timestamp | 0 ? function(tamp) { return tamp < 864e5 ? +new Date() + tamp * 864e5 :tamp; }(parseInt(timestamp)) :+new Date()); return jeDt.parse([ De.getFullYear(), De.getMonth() + 1, De.getDate() ], [ De.getHours(), De.getMinutes(), De.getSeconds() ], format); }; jeDt.montharr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]; //判断元素类型 jeDt.isValHtml = function(that) { return /textarea|input/.test(that.tagName.toLocaleLowerCase()); }; jeDt.weeks = [ "日", "一", "二", "三", "四", "五", "六" ]; //节日 jeDt.festival = function(md, n) { var str = ""; switch (md) { case "01.01": str = "元旦"; break; case "02.14": str = "情人"; break; case "03.08": str = "妇女"; break; case "04.05": str = "清明"; break; case "05.01": str = "劳动"; break; case "06.01": str = "儿童"; break; case "08.01": str = "建军"; break; case "09.10": str = "教师"; break; case "10.01": str = "国庆"; break; case "12.24": str = "平安"; break; case "12.25": str = "圣诞"; break; default: str = n; break; } return str; }; //补齐数位 jeDt.digit = function(num) { return num < 10 ? "0" + (num | 0) :num; }; //显示隐藏层 jeDt.shdeCell = function(type) { type ? QD(Cell)[0].style.display = "none" :QD(Cell)[0].style.display = "block"; }; var config = { dateCell:"#dateval", format:"YYYY-MM-DD hh:mm:ss", //日期格式 minDate:"1900-01-01 00:00:00", //最小日期 maxDate:"2099-12-31 23:59:59", //最大日期 isinitVal:false, //是否初始化时间 isTime:false, //是否开启时间选择 isClear:true, //是否显示清空 festival:false, //是否显示节日 zIndex:999, //弹出层的层级高度 choosefun:function(val) {}, clearfun:function(val) {}, okfun:function(val) {} }, InitDate = function(options) { var that = this, newConf = JSON.parse(JSON.stringify(config)); that.config = jeDt.extend(newConf, options); that.init(); }; var jeDate = function(options) { return new InitDate(options || {}); }; InitDate.prototype = { init:function() { var that = this, opts = that.config, self = QD(opts.dateCell)[0], elem, devt, even = window.event, target; var dateDiv = doc.createElement("div"); if (!QD(Cell)[0]) { dateDiv.className = dateDiv.id = Cell.replace("#", ""); dateDiv.style.zIndex = opts.zIndex; doc.body.appendChild(dateDiv); } try { target = even.target || even.srcElement || {}; } catch (e) { target = {}; } elem = opts.dateCell ? QD(opts.dateCell)[0] :target; var nowDateVal = jeDt.nowDate(null, opts.format); if (opts.isinitVal) { (jeDt.val(self) || jeDt.text(self)) == "" ? jeDt.isValHtml(self) ? jeDt.val(self, nowDateVal) :jeDt.text(self, nowDateVal) :jeDt.isValHtml(self) ? jeDt.val(self) :jeDt.text(self); } if (even && target.tagName) { if (!elem || elem === jeDt.elem) return; jeDt.stopMosup(even.type, elem); jeDt.stopmp(even); that.setHtml(opts, self); } else { devt = opts.event || "click"; jeDt.each((elem.length | 0) > 0 ? elem :[ elem ], function(ii, cel) { jeDt.stopMosup(devt, that); jeDt.on(cel, devt, function(ev) { jeDt.stopmp(ev); if (cel !== jeDt.elem) that.setHtml(opts, self); }); }); } }, setHtml:function(opts, self) { var that = this, weekHtml = "", date = new Date(), nowDateVal = jeDt.nowDate(null, opts.format), isformat = opts.format.match(/\w+|d+/g).join("-") == "YYYY-MM" ? true :false; var initVal = opts.isinitVal ? jeDt.isValHtml(self) ? jeDt.val(self) :jeDt.text(self) :(jeDt.val(self) || jeDt.text(self)) == "" ? nowDateVal :jeDt.isValHtml(self) ? jeDt.val(self) :jeDt.text(self); if (jeDt.val(self) != "" || jeDt.text(self) != "") { var arrTime = initVal.match(/\d+/g); } else { var arrTime = [ date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds() ]; } var topymSty = !isformat ? '
' + '
' :'
'; var datetopStr = '
' + topymSty + "
"; var dateymList = !isformat ? '" :''; var dateriList = '
    '; var bothmsStr = !isformat ? '' + '
    清空今天确认
    ' :'
    清空本月确认
    '; var datebotStr = '
    ' + bothmsStr + "
    "; var dateHtmStr = isformat ? datetopStr + dateymList + datebotStr :datetopStr + dateymList + dateriList + datebotStr + '
    '; jeDt.html(QD(Cell)[0], dateHtmStr); opts.isClear ? "" :QD(Cell + " .jedatebot .jedateclear")[0].style.display = "none"; if (opts.isTime) { var dhmsArr = jeDt.val(self) != "" || jeDt.text(self) != "" ? [ arrTime[3], arrTime[4], arrTime[5] ] :[ date.getHours(), date.getMinutes() + 1, date.getSeconds() ]; jeDt.each(QD(Cell + " .jedatebot .jedatehms em"), function(i, cls) { jeDt.html(cls, jeDt.digit(dhmsArr[i])); }); } else { if (!isformat) QD(Cell + " .jedatebot .jedatehms")[0].style.display = "none"; QD(Cell + " .jedatebot .jedatebtn")[0].style.width = "100%"; } if (!isformat) { for (var i = 0; i < jeDt.weeks.length; i++) { weekHtml += '
  1. ' + jeDt.weeks[i] + "
  2. "; } jeDt.html(QD(Cell + " .jedaol")[0], weekHtml); that.getDateStr(arrTime[0], arrTime[1], arrTime[2]); that.YearAndMonth(that, opts, self, arrTime); } else { jeDt.html(QD(Cell + " .jedaym")[0], that.onlyYMStr(arrTime[0], arrTime[1])); jeDt.text(QD(Cell + " .jedateym .jedateyearmonth")[0], arrTime[0] + "年" + jeDt.digit(arrTime[1]) + "月"); that.onlyYMevents(that, opts, self, arrTime); } jeDt.shdeCell(false); that.orien(QD(Cell)[0], self); that.events(that, opts, self, arrTime); }, onlyYMStr:function(y, m) { var onlyYM = ""; jeDt.each(jeDt.montharr, function(i, val) { onlyYM += "
  3. ' + y + "年" + jeDt.digit(val) + "月
  4. "; }); return onlyYM; }, onlyYMevents:function(that, opts, self, arrTime) { var ymPre = QD(Cell + " .jedateym .ymprev")[0], ymNext = QD(Cell + " .jedateym .ymnext")[0], ony = parseInt(arrTime[0]), onm = parseInt(arrTime[1]); jeDt.each([ ymPre, ymNext ], function(i, cls) { jeDt.on(cls, "click", function(ev) { jeDt.stopmp(ev); var ym = cls == ymPre ? ony -= 1 :ony += 1; jeDt.html(QD(Cell + " .jedaym")[0], that.onlyYMStr(ym, onm)); that.events(that, opts, self, arrTime); }); }); }, //方位辨别 orien:function(obj, self, pos) { var tops, rect = self.getBoundingClientRect(); obj.style.left = rect.left + (pos ? 0 :jeDt.scroll(1)) + "px"; tops = (rect.bottom + obj.offsetHeight / 1.5 <= jeDt.winarea()) ? rect.bottom - 1 : rect.top > obj.offsetHeight / 1.5 ? rect.top - obj.offsetHeight + 1 :jeDt.winarea() - obj.offsetHeight; obj.style.top = Math.max(tops + (pos ? 0 :jeDt.scroll()) + 1, 1) + "px"; }, getDateStr:function(y, m, d) { var that = this, opts = that.config, dayStr = "", m = jeDt.digit(m); jeDt.text(QD(Cell + " .jedateyear")[0], y + "年").attr(QD(Cell + " .jedateyear")[0], "data-year", y); jeDt.text(QD(Cell + " .jedatemonth")[0], m + "月").attr(QD(Cell + " .jedatemonth")[0], "data-month", m); //是否显示节日 var isfestival = function(day, n) { return opts.festival ? jeDt.festival(day, n) :n; }; var parseArr = function(str) { var timeArr = str.split(" "); return timeArr[0].split("-"); }; //先得到当前月第一天是星期几. var date = setMonthDays(y, m), weekday = new Date(y, parseInt(m) - 1, 1).getDay(); //根据这个星期算前面几天的上个月最后几天. var pervLastDay = weekday != 0 ? weekday :weekday + 7; //得到上个月最后一天; var pervMonthlastDay = getPervMonthLastDay(y, m), currentMonthDays = getPervMonthLastDay(y, parseInt(m) + 1); //上月最后几天循环 var lastdays = pervMonthlastDay - pervLastDay; //判断是否超出允许的日期范围 var startDay = 1, minArr = parseArr(opts.minDate), maxArr = parseArr(opts.maxDate), endDay = currentMonthDays, thisDate = new Date(y, m, d), firstDate = new Date(y, m, 1), lastDate = new Date(y, m, currentMonthDays), minTime = new Date(minArr[0], minArr[1], minArr[2]), maxTime = new Date(maxArr[0], maxArr[1], maxArr[2]), minDateDay = minTime.getDate(); if (minTime > lastDate) { startDay = parseInt(currentMonthDays) + 1; } else if (minTime >= firstDate && minTime <= lastDate) { startDay = minDateDay; } else if (minTime >= firstDate) {} if (maxTime) { var maxDateDay = maxTime.getDate(); if (maxTime < firstDate) { endDay = startDay; } else if (maxTime >= firstDate && maxTime <= lastDate) { endDay = maxDateDay; } } //循环上月剩余的天数 for (var p = pervLastDay - 1; p >= 0; p--) { var py, pm, preCls, preDays = jeDt.digit(pervMonthlastDay - p); m == 1 ? (py = parseInt(y) - 1, pm = 13) :(py = y, pm = m); var thatpretm = parseInt(py.toString() + jeDt.digit(parseInt(pm) - 1).toString() + preDays.toString()), minpretm = parseInt(minArr[0].toString() + jeDt.digit(minArr[1]).toString() + jeDt.digit(minArr[2]).toString()), maxnexttm = parseInt(maxArr[0].toString() + jeDt.digit(maxArr[1]).toString() + jeDt.digit(maxArr[2]).toString()); preCls = thatpretm >= minpretm && thatpretm <= maxnexttm ? "prevdate" :preCls = "disabled"; dayStr += "
  5. " + isfestival(parseInt(pm) - 1 + "." + preDays, preDays) + "
  6. "; } //循环本月的天数,将日期按允许的范围分三段拼接 for (var i = 1; i < startDay; i++) { i = jeDt.digit(i); dayStr += '
  7. ' + isfestival(m + "." + i, i) + "
  8. "; } for (var j = startDay; j <= endDay; j++) { var current = ""; j = jeDt.digit(j); if (/*y==value.year && m==value.month+1&& */ d == j) { current = "action"; } dayStr += '
  9. ' + isfestival(m + "." + j, j) + "
  10. "; } for (var k = endDay + 1; k <= currentMonthDays; k++) { k = jeDt.digit(k); dayStr += '
  11. ' + isfestival(m + "." + k, k) + "
  12. "; } //循环补上下个月的开始几天 var nextDayArr = [], nextMonthStartDays = 42 - pervLastDay - setMonthDays(y, m); for (var n = 1; n <= nextMonthStartDays; n++) { var ny, nm, nextCls; n = jeDt.digit(n); m >= 12 ? (ny = parseInt(y) + 1, nm = 0) :(ny = y, nm = m); var thatnexttm = parseInt(ny.toString() + jeDt.digit(parseInt(nm) + 1).toString() + jeDt.digit(n).toString()), minnexttm = parseInt(minArr[0].toString() + jeDt.digit(minArr[1]).toString() + jeDt.digit(minArr[2]).toString()), maxnexttm = parseInt(maxArr[0].toString() + jeDt.digit(maxArr[1]).toString() + jeDt.digit(maxArr[2]).toString()); nextCls = thatnexttm <= maxnexttm && thatnexttm >= minnexttm ? "nextdate" :nextCls = "disabled"; dayStr += "
  13. " + isfestival(parseInt(nm) + 1 + "." + n, n) + "
  14. "; } jeDt.html(QD(Cell + " .jedaul")[0], dayStr); jeDt.attr(QD(Cell + " .monthprev")[0], "data-y", jeDt.digit(parseInt(m) - 1)); jeDt.attr(QD(Cell + " .monthnext")[0], "data-y", jeDt.digit(parseInt(m) + 1)); //计算某年某月有多少天,如果是二月,闰年28天否则29天 function setMonthDays(year, month) { var er = year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 :28; return [ 31, er, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ][month - 1]; } //得到指定月的上个月最后一天传进来按 12月算 function getPervMonthLastDay(year, month) { //当月就是 yue-1 也就是计算机里面的0-11月份,那么算上个月的最后一天就是当月的0天 return parseInt(new Date(year, month - 1, 0).getDate()); } }, events:function(that, opts, self, arrTime) { var yearPre = QD(Cell + " .yearprev")[0], yearNext = QD(Cell + " .yearnext")[0], monthPre = QD(Cell + " .monthprev")[0], monthNext = QD(Cell + " .monthnext")[0], newDate = new Date(), jedateyear = QD(Cell + " .jedateyear")[0], jedatemonth = QD(Cell + " .jedatemonth")[0], isformat = opts.format.match(/\w+|d+/g).join("-") == "YYYY-MM" ? true :false; if (!isformat) { //切换年 jeDt.each([ yearPre, yearNext ], function(i, cls) { jeDt.on(cls, "click", function(ev) { jeDt.stopmp(ev); var y = parseInt(jeDt.attr(jedateyear, "data-year")), m = parseInt(jeDt.attr(jedatemonth, "data-month")); cls == yearPre ? y -= 1 :y += 1; var d = newDate.toLocaleDateString() == y + "/" + m + "/" + newDate.getDate() ? arrTime[2] :1; that.getDateStr(y, m, d); that.clickLiDays(that, opts, self); }); }); //切换月 jeDt.each([ monthPre, monthNext ], function(i, cls) { jeDt.on(cls, "click", function(ev) { jeDt.stopmp(ev); var y = parseInt(jeDt.attr(jedateyear, "data-year")), m = parseInt(jeDt.attr(jedatemonth, "data-month")); if (cls == monthPre) { m == 1 ? (y -= 1, m = 12) :m -= 1; } else { m == 12 ? (y += 1, m = 1) :m += 1; } var d = newDate.toLocaleDateString() == y + "/" + m + "/" + newDate.getDate() ? arrTime[2] :1; that.getDateStr(y, m, d); that.clickLiDays(that, opts, self); }); }); //生成定位时分秒 jeDt.each(QD(Cell + " .jedatebot .jedatehms em"), function(i, cls) { jeDt.on(cls, "click", function() { var hmsStr = "", acton, hmscell = QD(Cell + " .jedateprophms")[0], hmslen = jeDt.attr(cls, "data-hms"), hmsstxt = [ "小时", "分钟", "秒数" ], removeEmpty = function() { jeDt.removeClass(hmscell, hmslen == 24 ? "jedateh" :"jedatems"); jeDt.html(hmscell, ""); }; hmsStr += '
    ' + hmsstxt[i] + '
    ×
    '; for (var h = 0; h < hmslen; h++) { h = jeDt.digit(h); acton = jeDt.text(cls) == h ? "action" :""; hmsStr += '

    ' + h + "

    "; } jeDt.removeClass(hmscell, hmslen == 24 ? "jedatems" :"jedateh").addClass(hmscell, hmslen == 24 ? "jedateh" :"jedatems"); jeDt.html(hmscell, hmsStr); jeDt.each(QD(Cell + " .jedateprophms p"), function(i, p) { jeDt.on(p, "click", function() { jeDt.html(cls, jeDt.digit(jeDt.text(p))); removeEmpty(); }); }); jeDt.each(QD(Cell + " .jedateprophms .jedatehmstitle"), function(i, c) { jeDt.on(c, "click", function() { removeEmpty(); }); }); }); }); //今天 jeDt.on(QD(Cell + " .jedatebot .jedatetodaymonth")[0], "click", function() { var toTime = [ newDate.getFullYear(), newDate.getMonth() + 1, newDate.getDate(), newDate.getHours(), newDate.getMinutes(), newDate.getSeconds() ]; var gettoDate = jeDt.parse([ toTime[0], toTime[1], toTime[2] ], [ toTime[3], toTime[4], toTime[5] ], opts.format); that.getDateStr(toTime[0], toTime[1], toTime[2]); jeDt.isValHtml(self) ? jeDt.val(self, gettoDate) :jeDt.text(self, gettoDate); jeDt.html(QD(Cell)[0], ""); jeDt.shdeCell(true); if (opts.choosefun === "function" || opts.choosefun != null) opts.choosefun(gettoDate); }); } else { jeDt.each(QD(Cell + " .jedaym li"), function(i, cls) { jeDt.on(cls, "click", function(ev) { jeDt.stopmp(ev); var atYM = jeDt.attr(cls, "data-onym").match(/\w+|d+/g); var getYMDate = jeDt.parse([ atYM[0], atYM[1], 1 ], [ 0, 0, 0 ], opts.format); jeDt.isValHtml(self) ? jeDt.val(self, getYMDate) :jeDt.text(self, getYMDate); jeDt.html(QD(Cell)[0], ""); jeDt.shdeCell(true); }); }); //本月 jeDt.on(QD(Cell + " .jedatebot .jedatetodaymonth")[0], "click", function() { var thisYM = jeDt.attr(QD(Cell + " .jedaym .action")[0], "data-onym").match(/\w+|d+/g), thisYMDate = jeDt.parse([ thisYM[0], thisYM[1], 1 ], [ 0, 0, 0 ], opts.format); jeDt.isValHtml(self) ? jeDt.val(self, thisYMDate) :jeDt.text(self, thisYMDate); jeDt.html(QD(Cell)[0], ""); jeDt.shdeCell(true); if (opts.choosefun === "function" || opts.choosefun != null) opts.choosefun(thisYMDate); }); } //清空 jeDt.on(QD(Cell + " .jedatebot .jedateclear")[0], "click", function() { var clearVal = jeDt.isValHtml(self) ? jeDt.val(self) :jeDt.text(self); jeDt.isValHtml(self) ? jeDt.val(self, "") :jeDt.text(self, ""); jeDt.html(QD(Cell)[0], ""); jeDt.shdeCell(true); if(clearVal != ""){ if (opts.clearfun === "function" || opts.clearfun != null) opts.clearfun(clearVal); } }); //确认 jeDt.on(QD(Cell + " .jedatebot .jedateok")[0], "click", function(ev) { jeDt.stopmp(ev); var jedCell = isformat ? QD(Cell + " .jedaym li") :QD(Cell + " .jedaul li") if (!isformat) { var okTimeArr = [], okTime = [ parseInt(jeDt.attr(jedateyear, "data-year")), parseInt(jeDt.attr(jedatemonth, "data-month")), arrTime[2] ]; jeDt.each(QD(Cell + " .jedatehms em"), function(l, tval) { okTimeArr.push(jeDt.text(tval)); }); var okVal = jeDt.val(self) != "" || jeDt.text(self) != "" ? jeDt.parse([ okTime[0], okTime[1], okTime[2] ], [ okTimeArr[0], okTimeArr[1], okTimeArr[2] ], opts.format) :""; that.getDateStr(okTime[0], okTime[1], okTime[2]); }else{ var jedYM = jeDt.val(self) != "" || jeDt.text(self) != "" ? jeDt.attr(QD(Cell + " .jedaym .action")[0], "data-onym").match(/\w+|d+/g) :"", okVal = jeDt.val(self) != "" || jeDt.text(self) != "" ? jeDt.parse([ jedYM[0], jedYM[1], 1 ], [ 0, 0, 0 ], opts.format) :"" } jeDt.each(jedCell, function(i, cls) { if (jeDt.attr(cls, "class") == "action") { jeDt.isValHtml(self) ? jeDt.val(self, okVal) :jeDt.text(self, okVal); } }); jeDt.html(QD(Cell)[0], ""); jeDt.shdeCell(true); if(okVal != ""){ if (opts.okfun === "function" || opts.okfun != null) opts.okfun(okVal); } }); //点击空白处隐藏 jeDt.on(document, "click", function() { jeDt.shdeCell(true); jeDt.html(QD(Cell)[0], ""); }); jeDt.on(QD(Cell)[0], "click", function(ev) { jeDt.stopmp(ev); }); that.clickLiDays(that, opts, self); }, //下拉选择年和月 YearAndMonth:function(that, opts, self, arrTime) { var jetopym = QD(Cell + " .jedatetopym")[0], jedateyy = QD(Cell + " .jedateyy")[0], jedatemm = QD(Cell + " .jedatemm")[0], jedateyear = QD(Cell + " .jedateyy .jedateyear")[0], jedatemonth = QD(Cell + " .jedatemm .jedatemonth")[0], mchri = QD(Cell + " .jedateymchri")[0], mchle = QD(Cell + " .jedateymchle")[0]; function eachYears(YY) { var eachStr = ""; jeDt.each(new Array(15), function(i) { if (i === 7) { var getyear = jeDt.attr(jedateyear, "data-year"); eachStr += "
  15. ' + YY + "年
  16. "; } else { eachStr += '
  17. ' + (YY - 7 + i) + "年
  18. "; } }); return eachStr; } function setYearMonth(YY, ymlen) { var ymStr = ""; if (ymlen == 12) { jeDt.each(jeDt.montharr, function(i, val) { var getmonth = jeDt.attr(jedatemonth, "data-month"), val = jeDt.digit(val); ymStr += "
  19. ' + val + "月
  20. "; }); jeDt.each([ mchri, mchle ], function(c, cls) { cls.style.display = "none"; }); } else { ymStr = eachYears(YY); jeDt.each([ mchri, mchle ], function(c, cls) { cls.style.display = "block"; }); } jeDt.removeClass(jetopym, ymlen == 12 ? "jedatesety" :"jedatesetm").addClass(jetopym, ymlen == 12 ? "jedatesetm" :"jedatesety"); jeDt.html(QD(Cell + " .jedatetopym .ymdropul")[0], ymStr); jetopym.style.display = "block"; } function clickLiYears(year) { jeDt.each(QD(Cell + " .ymdropul li"), function(i, cls) { jeDt.on(cls, "click", function(ev) { var Years = jeDt.attr(this, "data-y"), Months = jeDt.attr(jedatemonth, "data-month"); jeDt.attr(year, "data-year", Years); jeDt.html(year, Years); jetopym.style.display = "none"; that.getDateStr(Years, Months, arrTime[2]); that.clickLiDays(that, opts, self); }); }); } //下拉选择年 jeDt.on(jedateyy, "click", function() { var YMlen = parseInt(jeDt.attr(jedateyy, "data-ym")), yearAttr = parseInt(jeDt.attr(jedateyear, "data-year")); setYearMonth(yearAttr, YMlen); clickLiYears(jedateyear); }); //下拉选择月 jeDt.on(jedatemm, "click", function() { var YMlen = parseInt(jeDt.attr(jedatemm, "data-ym")), yearAttr = parseInt(jeDt.attr(jedateyear, "data-year")); setYearMonth(yearAttr, YMlen); jeDt.each(QD(Cell + " .ymdropul li"), function(i, cls) { jeDt.on(cls, "click", function(ev) { var Years = jeDt.attr(jedateyear, "data-year"), Months = jeDt.attr(this, "data-m"); jeDt.attr(jedatemonth, "data-month", Months); jeDt.html(jedatemonth, Months); jetopym.style.display = "none"; that.getDateStr(Years, Months, arrTime[2]); that.clickLiDays(that, opts, self); }); }); }); //关闭下拉选择 jeDt.on(QD(Cell + " .jedateymchok")[0], "click", function(ev) { jeDt.stopmp(ev); jetopym.style.display = "none"; }); var yearMch = parseInt(jeDt.attr(jedateyear, "data-year")); jeDt.each([ mchle, mchri ], function(lr, cls) { jeDt.on(cls, "click", function(ev) { jeDt.stopmp(ev); lr == 0 ? yearMch -= 15 :yearMch += 15; var mchStr = eachYears(yearMch); jeDt.html(QD(Cell + " .jedatetopym .ymdropul")[0], mchStr); clickLiYears(jedateyear); }); }); }, //选择日 clickLiDays:function(that, opts, self) { jeDt.each(QD(Cell + " .jedaul li"), function(i, cls) { jeDt.on(cls, "click", function(ev) { if (jeDt.hasClass(cls, "disabled")) return; jeDt.stopmp(ev); var liTms = []; jeDt.each(QD(Cell + " .jedatehms em"), function(l, tval) { liTms.push(jeDt.text(tval)); }); var aty = parseInt(jeDt.attr(cls, "data-y")) | 0, atm = parseInt(jeDt.attr(cls, "data-m")) | 0, atd = parseInt(jeDt.attr(cls, "data-d")) | 0; var getParDate = jeDt.parse([ aty, atm, atd ], [ liTms[0], liTms[1], liTms[2] ], opts.format); that.getDateStr(aty, atm, atd); jeDt.isValHtml(self) ? jeDt.val(self, getParDate) :jeDt.text(self, getParDate); jeDt.html(QD(Cell)[0], ""); jeDt.shdeCell(true); if (opts.choosefun === "function" || opts.choosefun != null) { opts.choosefun(getParDate); } }); }); } }; jeDt.getPath = (function(){ var js = document.scripts, jsPath = js[js.length - 1].src; return jsPath.substring(0, jsPath.lastIndexOf("/") + 1); }()); jeDt.creatlink = function(lib){ var link = document.createElement('link'); link.type = 'text/css'; link.rel = 'stylesheet'; link.href = jeDt.getPath +'skin/'+ lib + '.css'; link.id = 'jeDateSkin'; QD('head')[0].appendChild(link); link = null; }; jeDt.creatlink('jedate'); jeDate.skin = function(lib){ QD('#jeDateSkin')[0].parentNode.removeChild(QD('#jeDateSkin')[0]); jeDt.creatlink(lib); }; //返回指定日期 jeDate.now = function(num) { var dd = new Date(); dd.setDate(dd.getDate() + num); var y = dd.getFullYear(), m = dd.getMonth() + 1, d = dd.getDate(); return y + "-" + m + "-" + d; }; "function" === typeof define ? define(function() { return jeDate; }) :window.jeDate = jeDate; })(window);