/*** * 漫画jquery时间插件 * 编写时间:2012年7月14号 * version:manhuadate.1.0.js ***/ $(function() { $.fn.manhuadate = function(options) { var defaults = { event : "click", //插件绑定的响应事件 left : 0, //弹出时间停靠的左边位置 top : 22, //弹出时间停靠的上边位置 fuhao : "-", //日期之间的连接符号 istime : false, //是否开启时间值默认为false beginy : 1949, //年份的开始默认为1949 endy : 2049 //年份的结束默认为2049 }; var options = $.extend(defaults,options); var stc; if($("#calender").length<=0){ $("body").prepend("
"); } var $mhinput = $(this); var istoday = true;//是否为今天默认为是 var date = new date();//获得时间对象 var nowyear = date.getfullyear();//获得当前年份 var nowmonth = date.getmonth() + 1;//获得当前月份 var today = date.getdate();//获得当前天数 var nowweek = new date(nowyear, nowmonth - 1, 1).getday();//获得当前星期 var nowlastday = getmonthnum(nowmonth, nowyear);//获得最后一天 //年、月下拉框的初始化 for(var i=options.beginy; i<=options.endy; i++){ $("").appendto($("#year")); } for(var i=1; i<=12; i++){ $("").appendto($("#month")); } manhuadate(nowyear, nowmonth, nowweek, nowlastday);//初始化为当前日期 //上一月绑定点击事件 $("#premonth").click(function() { istoday = false; var year = parseint($("#year").val()); var month = parseint($("#month").val()); month = month - 1; if (month < 1) { month = 12; year = year - 1; } if(nowyear==year && nowmonth==month){ istoday = true; } var week = new date(year, month - 1, 1).getday(); var lastday = getmonthnum(month, year); manhuadate(year, month, week, lastday); }); //年下拉框的改变事件 $("#year").change(function() { istoday = false; var year = parseint($(this).val()); var month = parseint($("#month").val()); if(nowyear==year && nowmonth==month){ istoday = true; } var week = new date(year, month - 1, 1).getday(); var lastday = getmonthnum(month, year); manhuadate(year, month, week, lastday); }); //月下拉框的改变事件 $("#month").change(function() { istoday = false; var year = parseint($("#year").val()); var month = parseint($(this).val()); if(nowyear==year && nowmonth==month){ istoday = true; } var week = new date(year, month - 1, 1).getday(); var lastday = getmonthnum(month, year); manhuadate(year, month, week, lastday); }); //下一个月的点击事件 $("#nextmonth").click(function() { istoday = false; var year = parseint($("#year").val()); var month = parseint($("#month").val()); month = parseint(month) + 1; if (parseint(month) > 12) { month = 1; year = parseint(year) + 1; } if(nowyear==year && nowmonth==month){ istoday = true; } var week = new date(year, month - 1, 1).getday(); var lastday = getmonthnum(month, year); manhuadate(year, month, week, lastday); }); //初始化日历 function manhuadate(year, month, week, lastday) { $("#year").val(year); $("#month").val(month) var table = document.getelementbyid("calender"); var n = 1; for (var j = 0; j < week; j++) { table.rows[1].cells[j].innerhtml = " " } for (var j = week; j < 7; j++) { if (n == today && istoday) { table.rows[1].cells[j].classname="tdtoday"; }else { table.rows[1].cells[j].classname=""; } table.rows[1].cells[j].innerhtml = n; n++; } for (var i = 2; i < 7; i++) { for (j = 0; j < 7; j++) { if (n > lastday) { table.rows[i].cells[j].innerhtml = " " } else { if (n == today && istoday) { table.rows[i].cells[j].classname="tdtoday"; }else { table.rows[i].cells[j].classname=""; } table.rows[i].cells[j].innerhtml = n; n++; } } } } //获得月份的天数 function getmonthnum(month, year) { month = month - 1; var leapyear = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? true: false; var monthnum; switch (parseint(month)) { case 0: case 2: case 4: case 6: case 7: case 9: case 11: monthnum = 31; break; case 3: case 5: case 8: case 10: monthnum = 30; break; case 1: monthnum = leapyear ? 29: 28; } return monthnum; } //每一列的悬挂事件改变当前样式 $("#calender td:not(.tdtoday)").hover(function() { $(this).addclass("hover") },function() { $(this).removeclass("hover"); }); //点击时间列表事件 $("#calender td").die().live("click",function() { var dv = $(this).html(); if (dv != " "){ var str = ""; if (options.istime){ var nd = new date(); str = $("#year").val() + options.fuhao + $("#month").val() + options.fuhao + dv + " "+ nd.gethours()+":"+nd.getminutes()+":"+nd.getseconds(); }else{ str = $("#year").val() + options.fuhao + $("#month").val() + options.fuhao + dv; } $("input.datevisited").val(str); $("input.datevisited").removeclass('datevisited') $(".calender").hide(); } }); //文本框绑定事件 $mhinput.live(options.event,function(e){ $(this).addclass("datevisited"); if(stc){ cleartimeout(stc);//清除定时器 } var iof = $(this).offset(); $(".calender").css({ "left" : iof.left+options.left,"top" : iof.top+options.top }); $(".calender").show(); }); //当鼠标离开控件上面的时候延迟3秒关闭 $(".calender").live("mouseleave",function(){ stc = settimeout(function (){ $(".calender").hide(); cleartimeout(stc); },3000); }); //当鼠标移到控件上面的时候显示 $(".calender").live("mousemove",function(){ if(stc){ cleartimeout(stc);//清除定时器 } $(this).show(); }); //点击年选择下拉框的时候清除定时器阻止控件层关闭 $("#year").die().live("click",function(){ if(stc){ cleartimeout(stc);//清除定时器 } }); //点击月选择下拉框的时候清除定时器阻止控件层关闭 $("#month").die().live("click",function(){ if(stc){ cleartimeout(stc);//清除定时器 } }); }; });