1 <html> 2 <head> 3 <title>很漂亮、兼容火狐的Js日期选择,自动填充到输入框</title> 4 <meta http-equiv="content-Type" content="text/html;charset=gb2312"> 5 <style type="text/css"> 6 body{font-size:12px;font-family:Verdana,Arial,"宋体";} 7 a:link {color:#464646;text-decoration:none;} 8 a:visited {color:#464646;text-decoration:none;} 9 a:hover{color:#ed145b;text-decoration:underline;} 10 a:active{color:#ed145b;text-decoration:underline;} 11 td{font-size:12px} 12 /*DateCSS样式*/ 13 .header {font: 12px Arial, Tahoma !important;font-weight: bold !important;font: 11px Arial, Tahoma;font-weight: bold;color: #154BA0;background:#C2DEED;height: 25px;padding-left: 10px; 14 } 15 .header td {padding-left: 10px;} 16 .header a {color: #154BA0;} 17 .header input {background:none;vertical-align: middle;height: 16px;} 18 .category {font: 12px Arial, Tahoma !important;font: 11px Arial, Tahoma;color: #92A05A;height:20px;background-color: #FFFFD9;} 19 .category td {border-bottom: 1px solid #DEDEB8;} 20 .expire, .expire a:link, .expire a:visited {color: #999999;} 21 .default, .default a:link, .default a:visited {color: #000000;} 22 .checked, .checked a:link, .checked a:visited {color: #FF0000;} 23 .today, .today a:link, .today a:visited {color: #00BB00;} 24 #calendar_year {display: none;line-height: 130%;background: #FFFFFF;position: absolute;z-index: 10;} 25 #calendar_year .col {float: left;background: #FFFFFF;margin-left: 1px;border: 1px solid #86B9D6;padding: 4px;} 26 #calendar_month {display: none;background: #FFFFFF;line-height: 130%;border: 1px solid #86B9D6;padding: 4px;position: absolute;z-index: 11;} 27 .tableborder {background: white;border: 1px solid #86B9D6;} 28 #year,#month{padding-right:10px;} 29 </style> 30 <script language="javascript"> 31 //下面的代码段如果你页面里有,可以去掉 32 var ie =navigator.appName=="Microsoft Internet Explorer"?true:false; 33 function $(objID){ 34 return document.getElementById(objID); 35 } 36 </script> 37 </head> 38 <body> 39 40 <script type="text/javascript"> 41 var controlid = null; 42 var currdate = null; 43 var startdate = null; 44 var enddate = null; 45 var yy = null; 46 var mm = null; 47 var hh = null; 48 var ii = null; 49 var currday = null; 50 var addtime = false; 51 var today = new Date(); 52 var lastcheckedyear = false; 53 var lastcheckedmonth = false; 54 function _cancelBubble(event) { 55 e = event ? event : window.event ; 56 if(ie) { 57 e.cancelBubble = true; 58 } else { 59 e.stopPropagation(); 60 } 61 } 62 function getposition(obj) { 63 var r = new Array(); 64 r['x'] = obj.offsetLeft; 65 r['y'] = obj.offsetTop; 66 while(obj = obj.offsetParent) { 67 r['x'] += obj.offsetLeft; 68 r['y'] += obj.offsetTop; 69 } 70 return r; 71 } 72 function loadcalendar() { 73 s = ''; 74 s += '<div id="calendar" style="display:none; position:absolute; z-index:9;" onclick="_cancelBubble(event)">'; 75 if (ie) 76 { 77 s += '<iframe width="200" height="160" src="about:blank" style="position: absolute;z-index:-1;"></iframe>'; 78 } 79 s += '<div style=" 200px;"><table class="tableborder" cellspacing="0" cellpadding="0" width="100%" style="text-align: center">'; 80 s += '<tr align="center" class="header"><td class="header"><a href="#" onclick="refreshcalendar(yy, mm-1);return false" title="上一月"><<</a></td><td colspan="5" style="text-align: center" class="header"><a href="#" onclick="showdiv(\'year\');_cancelBubble(event);return false" title="点击选择年份" id="year"></a> - <a id="month" title="点击选择月份" href="#" onclick="showdiv(\'month\');_cancelBubble(event);return false"></a></td><td class="header"><A href="#" onclick="refreshcalendar(yy, mm+1);return false" title="下一月">>></A></td></tr>'; 81 s += '<tr class="category"><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>'; 82 for(var i = 0; i < 6; i++) { 83 s += '<tr class="altbg2">'; 84 for(var j = 1; j <= 7; j++) 85 s += "<td id=d" + (i * 7 + j) + " height=\"19\">0</td>"; 86 s += "</tr>"; 87 } 88 s += '<tr id="hourminute"><td colspan="7" align="center"><input type="text" size="1" value="" id="hour" onKeyUp=\'this.value=this.value > 23 ? 23 : zerofill(this.value);controlid.value=controlid.value.replace(/\\d+(\:\\d+)/ig, this.value+"$1")\'> 点 <input type="text" size="1" value="" id="minute" onKeyUp=\'this.value=this.value > 59 ? 59 : zerofill(this.value);controlid.value=controlid.value.replace(/(\\d+\:)\\d+/ig, "$1"+this.value)\'> 分</td></tr>'; 89 s += '</table></div></div>'; 90 s += '<div id="calendar_year" onclick="_cancelBubble(event)"><div class="col">'; 91 for(var k = 1930; k <= 2019; k++) { 92 s += k != 1930 && k % 10 == 0 ? '</div><div class="col">' : ''; 93 s += '<a href="#" onclick="refreshcalendar(' + k + ', mm);$(\'calendar_year\').style.display=\'none\';return false"><span' + (today.getFullYear() == k ? ' class="today"' : '') + ' id="calendar_year_' + k + '">' + k + '</span></a><br />'; 94 } 95 s += '</div></div>'; 96 s += '<div id="calendar_month" onclick="_cancelBubble(event)">'; 97 for(var k = 1; k <= 12; k++) { 98 s += '<a href="#" onclick="refreshcalendar(yy, ' + (k - 1) + ');$(\'calendar_month\').style.display=\'none\';return false"><span' + (today.getMonth()+1 == k ? ' class="today"' : '') + ' id="calendar_month_' + k + '">' + k + ( k < 10 ? ' ' : '') + ' 月</span></a><br />'; 99 } 100 s += '</div>'; 101 var nElement = document.createElement("div"); 102 nElement.innerHTML=s; 103 document.getElementsByTagName("body")[0].appendChild(nElement); 104 // http://www.codefans.net 105 document.onclick = function(event) { 106 $('calendar').style.display = 'none'; 107 $('calendar_year').style.display = 'none'; 108 $('calendar_month').style.display = 'none'; 109 } 110 $('calendar').onclick = function(event) { 111 _cancelBubble(event); 112 $('calendar_year').style.display = 'none'; 113 $('calendar_month').style.display = 'none'; 114 } 115 } 116 function parsedate(s) { 117 /(\d+)\-(\d+)\-(\d+)\s*(\d*):?(\d*)/.exec(s); 118 var m1 = (RegExp.$1 && RegExp.$1 > 1899 && RegExp.$1 < 2101) ? parseFloat(RegExp.$1) : today.getFullYear(); 119 var m2 = (RegExp.$2 && (RegExp.$2 > 0 && RegExp.$2 < 13)) ? parseFloat(RegExp.$2) : today.getMonth() + 1; 120 var m3 = (RegExp.$3 && (RegExp.$3 > 0 && RegExp.$3 < 32)) ? parseFloat(RegExp.$3) : today.getDate(); 121 var m4 = (RegExp.$4 && (RegExp.$4 > -1 && RegExp.$4 < 24)) ? parseFloat(RegExp.$4) : 0; 122 var m5 = (RegExp.$5 && (RegExp.$5 > -1 && RegExp.$5 < 60)) ? parseFloat(RegExp.$5) : 0; 123 /(\d+)\-(\d+)\-(\d+)\s*(\d*):?(\d*)/.exec("0000-00-00 00\:00"); 124 return new Date(m1, m2 - 1, m3, m4, m5); 125 } 126 function settime(d) { 127 $('calendar').style.display = 'none'; 128 controlid.value = yy + "-" + zerofill(mm + 1) + "-" + zerofill(d) + (addtime ? ' ' + zerofill($('hour').value) + ':' + zerofill($('minute').value) : ''); 129 } 130 function showcalendar(event, controlid1, addtime1, startdate1, enddate1) { 131 controlid = controlid1; 132 addtime = addtime1; 133 startdate = startdate1 ? parsedate(startdate1) : false; 134 enddate = enddate1 ? parsedate(enddate1) : false; 135 currday = controlid.value ? parsedate(controlid.value) : today; 136 hh = currday.getHours(); 137 ii = currday.getMinutes(); 138 var p = getposition(controlid); 139 $('calendar').style.display = 'block'; 140 $('calendar').style.left = p['x']+'px'; 141 $('calendar').style.top = (p['y'] + 20)+'px'; 142 _cancelBubble(event); 143 refreshcalendar(currday.getFullYear(), currday.getMonth()); 144 if(lastcheckedyear != false) { 145 $('calendar_year_' + lastcheckedyear).className = 'default'; 146 $('calendar_year_' + today.getFullYear()).className = 'today'; 147 } 148 if(lastcheckedmonth != false) { 149 $('calendar_month_' + lastcheckedmonth).className = 'default'; 150 $('calendar_month_' + (today.getMonth() + 1)).className = 'today'; 151 } 152 $('calendar_year_' + currday.getFullYear()).className = 'checked'; 153 $('calendar_month_' + (currday.getMonth() + 1)).className = 'checked'; 154 $('hourminute').style.display = addtime ? '' : 'none'; 155 lastcheckedyear = currday.getFullYear(); 156 lastcheckedmonth = currday.getMonth() + 1; 157 } 158 function refreshcalendar(y, m) { 159 var x = new Date(y, m, 1); 160 var mv = x.getDay(); 161 var d = x.getDate(); 162 var dd = null; 163 yy = x.getFullYear(); 164 mm = x.getMonth(); 165 $("year").innerHTML = yy; 166 $("month").innerHTML = mm + 1 > 9 ? (mm + 1) : '0' + (mm + 1); 167 for(var i = 1; i <= mv; i++) { 168 dd = $("d" + i); 169 dd.innerHTML = " "; 170 dd.className = ""; 171 } 172 while(x.getMonth() == mm) { 173 dd = $("d" + (d + mv)); 174 dd.innerHTML = '<a href="###" onclick="settime(' + d + ');return false">' + d + '</a>'; 175 if(x.getTime() < today.getTime() || (enddate && x.getTime() > enddate.getTime()) || (startdate && x.getTime() < startdate.getTime())) { 176 dd.className = 'expire'; 177 } else { 178 dd.className = 'default'; 179 } 180 if(x.getFullYear() == today.getFullYear() && x.getMonth() == today.getMonth() && x.getDate() == today.getDate()) { 181 dd.className = 'today'; 182 dd.firstChild.title = '今天'; 183 } 184 if(x.getFullYear() == currday.getFullYear() && x.getMonth() == currday.getMonth() && x.getDate() == currday.getDate()) { 185 dd.className = 'checked'; 186 } 187 x.setDate(++d); 188 } 189 while(d + mv <= 42) { 190 dd = $("d" + (d + mv)); 191 dd.innerHTML = " "; 192 d++; 193 } 194 if(addtime) { 195 $('hour').value = zerofill(hh); 196 $('minute').value = zerofill(ii); 197 } 198 } 199 function showdiv(id) { 200 var p = getposition($(id)); 201 $('calendar_' + id).style.left = p['x']+'px'; 202 $('calendar_' + id).style.top = (p['y'] + 16)+'px'; 203 $('calendar_' + id).style.display = 'block'; 204 } 205 function zerofill(s) { 206 var s = parseFloat(s.toString().replace(/(^[\s0]+)|(\s+$)/g, '')); 207 s = isNaN(s) ? 0 : s; 208 return (s < 10 ? '0' : '') + s.toString(); 209 } 210 loadcalendar(); 211 </script> 212 用法演示:<br>请选择日期:<input name="member.birth" type="text" value="2008-5-19" size="14" readonly onclick="showcalendar(event,this);" onfocus="showcalendar(event, this);if(this.value=='0000-00-00')this.value=''"> 213 </body> 214 </html>