这个时间组件以前发过一次,上次那个很烂,这次有时间了,把这个升级了,性能更好,完美兼容所有浏览器,ie6下拉select档不住的问题 也解决了.总之,差不多也算一个完美的时间组件, 在线demo nothingDemo 突然发现下面的代码里面有个运行代码可以看在线demo,就再最下面 然后贴出源码,只有一点简单的说明 复制代码 代码如下: Untitled Page <br>*{margin:0;padding:0;} <br>body, button, input, select, textarea { <br>font: 12px/1.125 Arial,Helvetica,sans-serif; <br>} <br>/*日期控件*/ <br>.pc_caldr{border:1px solid #ccc;background-color:#fff;z-index:10;width:175px;height:auto;position:absolute;color:#000;padding:5px;} <br>.pc_caldr .selector{height:24px;_padding:2px 0 2px;padding:2px 0 0;} <br>.pc_caldr .selector .month,.pc_caldr .selector .year{float:left;font-size:12px;width:80px;border:1px solid #CCC;height:19px;} <br>.pc_caldr .selector .year{width:84px;margin-left:10px;} <br>.pc_caldr .weeks,.pc_caldr .days{list-style:none;width:100%!important;margin:0;padding:0;} <br>.pc_caldr .weeks{height:18px;margin-bottom:2px;background:#b6d1f9;color:#fff;font-size:12px;} <br>.pc_caldr .days{height:auto;font-size:12px;font-family:Arial;} <br>.pc_caldr .weeks li,.pc_caldr .days li{float:left;height:20px;line-height:20px;text-align:center;width:25px;} <br>.pc_caldr .days li{background-color:none;} <br>.pc_caldr .days li a{display:block;text-decoration:none;height:100%;color:#43609c;transition:transform .5s;-moz-transition:-moz-transform .5s;-webkit-transition:-webkit-transform .5s;-o-transition:-o-transform .5s;padding:1px;} <br>.pc_caldr .days li a:link,.pc_caldr .days li a:visited,.pc_caldr .days li a:hover{text-decoration:none;} <br>.pc_caldr .days li a strong{font-weight:400;} <br>.pc_caldr .days li a:hover{background-color:#5d94e6;color:#fff;transform:rotate(180deg) scale(1.5);-moz-transform:rotate(360deg) scale(1.5);-webkit-transform:rotate(360deg) scale(1.5);-o-transform:rotate(360deg) scale(1.5);} <br>.pc_caldr .weeks li,.pc_caldr .days li,.pc_caldr .days li a{text-align:center;} <br>/*文本框*/ <br>.tiemin{width:120px;border:1px solid #f00;} <br>.inline-block {display :inline-block; zoom:1 ; *display: inline; vertical-align:middle ;} <br>.ie6iframe {background: none repeat scroll 0 0 #FFFFFF;left: -1px;filter:alpha(opacity=0);position: absolute;top: 0;z-index: -1;width:100%;height:150px;} <br> 作者nothing 挡住它nothing <br>var nothingTime = (function ($) { <br>var cache = { <br>obj: null, <br>calendar: null, <br>disappear: function () { //隐藏时间块 <br>cache.calendar.css("display", "none"); <br>}, <br>isLeap: function (year) { //闰年 <br>return (year % 100 == 0 ? (year % 400 == 0 ? 1 : 0) : (year % 4 == 0 ? 1 : 0)); <br>}, <br>isValid: function (d) { //是否在今天以前 <br>return (d.getTime() - (new Date()).getTime() < 0) ? true : false; <BR>}, <BR>td: new Date(), <BR>createData: function (year, month) { <BR>var n1 = new Date(year, month, 1), <BR>firstday = n1.getDay(), <BR>mdays = [31, 28 this.isLeap(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], <BR>ul = document.createElement("ul"), li; <BR>ul.className = "days"; <BR>$("#calendar").find(".days").remove(); <BR>for (var i = firstday;i--;) { //建立前面无效的几天 <BR>ul.appendChild(document.createElement("li")); <BR>} <BR>for (var j = 1; j <= mdays[month]; j ) { <BR>if (this.isValid(new Date(year, month, j))) { //今天以前的日子 <BR>li = document.createElement("li"); <BR>li.innerHTML = "<a href='javascript:void(0)'>" j "</a>"; <br>ul.appendChild(li); <br>} else { <br>li = document.createElement("li"); <br>li.innerHTML = j; <br>ul.appendChild(li); <br>} <br>} <br>this.calendar[0].appendChild(ul); <br>}, <br>change: function () { //给下拉列表绑定时间 <br>var a = $("#calendar .month"), <br>b = $("#calendar .year"); <br>a.change(function () { <br>cache.createData(b.attr("value"), $(this).attr("value")); <br>}); <br>b.change(function () { <br>cache.createData($(this).attr("value"), a.attr("value")); <br>}); <br>cache.calendar.delegate(".days a", "click", function () { <br>var day = b.attr("value") "-" (parseInt(a.attr("value")) 1) "-" this.innerHTML; <br>cache.obj.val(day); <br>cache.disappear(); <br>}); <br>}, <br>bodyClickDisappear: function () { <br>setTimeout(function () { <br>$("body").bind("click", cache.disappear); <br>}, "200"); <br>}, <br>calendarClick: function () { <br>cache.calendar.click(function (e) { <br>e.stopPropagation(); <br>}); <br>} <br>}, <br>CreateTime = function (obj) { <br>cache.obj = obj; <br>var of = cache.obj.offset(); <br>if (document.getElementById("calendar")) { <br>} else { <br>var se = "<div class='selector'><select class='month'><option value='0'>一月</option><option value='1'>二月</option><option value='2'>三月</option><option value='3'>四月</option><option value='4'>五月</option><option value='5'>六月</option><option value='6'>七月</option><option value='7'>八月</option><option value='8'>九月</option><option value='9'>十月</option><option value='10'>十一月</option><option value='11'>十二月</option></select><select class='year'><option value='2011'>2011</option><option value='2012'>2012</option></select></div><ul class='weeks'><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul>"; <br>$("<div>", { id: "calendar", html: se, "class": "pc_caldr" }).appendTo(document.body); <br>cache.calendar = $("#calendar"); <br>if (/msie 6.0/i.test(navigator.userAgent)) { <br>var iframe = document.createElement("iframe"); <br>iframe.className = "ie6iframe"; <br>cache.calendar[0].appendChild(iframe); <br>} <br>cache.change(); <br>cache.bodyClickDisappear(); <br>cache.calendarClick(); <br>} <br>cache.createData(cache.td.getFullYear(), cache.td.getMonth()); <br>cache.calendar.find(".year").attr("value", cache.td.getFullYear()); <br>cache.calendar.find(".month").attr("value", cache.td.getMonth()); <br>cache.calendar.css({ left: of.left, top: of.top cache.obj.height() 2, display: "block" }); <br>}; <br>return function (obj) { <br>CreateTime(obj); <br>}; <br>})(jQuery); <br>//使用方法 <br>$("input.tiemin").focus(function (e) { <br>nothingTime($(this)); <br>}).click(function (e) { <br>e.stopPropagation(); <br>}); <br> OK,这个时间组件到此为止,下篇我应该讲点如何跟上js代码的脚步,ECMAScript5,我会试着模仿里面的方法,然后在ie6 7 8中运行,这样,前沿的js方法 我们照样不误.