根据一篇js日历插件改写的
时间:2014-04-30 00:45:05
收藏:0
阅读:569
1 <!-- 2 控件调用示例: 3 --> 4 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 7 <html xmlns="http://www.w3.org/1999/xhtml"> 8 9 <head> 10 11 <script src="date.js" language="javascript" ></script> 12 13 </head> 14 15 <body> 16 17 <input type="text" style="width:70px" onfocus="HS_setDate(this)"> 18 19 20 21 22 </body> 23 24 </html> 25 26 27 //date.js 28 29 30 function HS_DateAdd(interval,number,date){ 31 32 number = parseInt(number); 33 34 if (typeof(date)=="string"){var date = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2])} 35 36 if (typeof(date)=="object"){var date = date} 37 38 switch(interval){ 39 40 case "y":return new Date(date.getFullYear()+number,date.getMonth(),date.getDate()); break; 41 42 case "m":return new Date(date.getFullYear(),date.getMonth()+number,checkDate(date.getFullYear(),date.getMonth()+number,date.getDate())); break; 43 44 case "d":return new Date(date.getFullYear(),date.getMonth(),date.getDate()+number); break; 45 46 case "w":return new Date(date.getFullYear(),date.getMonth(),7*number+date.getDate()); break; 47 48 } 49 50 } 51 52 function checkDate(year,month,date){ 53 54 var enddate = ["31","28","31","30","31","30","31","31","30","31","30","31"]; 55 56 var returnDate = ""; 57 58 if (year%4==0){enddate[1]="29"} 59 60 if (date>enddate[month]){returnDate = enddate[month]}else{returnDate = date} 61 62 return returnDate; 63 64 } 65 66 function WeekDay(date){ 67 68 var theDate; 69 70 if (typeof(date)=="string"){theDate = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2]);} 71 72 if (typeof(date)=="object"){theDate = date} 73 74 return theDate.getDay(); 75 76 } 77 78 function HS_calender(){ 79 80 var lis = ""; 81 82 var style = ""; 83 84 85 86 style +="<style type=‘text/css‘>"; 87 88 style +=".calender { width:170px; height:auto; font-size:12px; margin-right:14px; background:url(calenderbg.gif) no-repeat right center #fff; border:1px solid #397EAE; padding:1px;z-index:10009;}"; 89 90 style +=".calender ul {list-style-type:none; margin:0; padding:0;}"; 91 92 style +=".calender .day { background-color:#EDF5FF; height:20px;}"; 93 94 style +=".calender .day li,.calender .date li{ float:left; width:14%; height:20px; line-height:20px; text-align:center}"; 95 96 style +=".calender li a { text-decoration:none; font-family:Tahoma; font-size:11px; color:#333}"; 97 98 style +=".calender li a:hover { color:#f30; text-decoration:underline}"; 99 100 style +=".calender li a.hasArticle {font-weight:bold; color:#f60 !important}"; 101 102 style +=".lastMonthDate, .nextMonthDate {color:#bbb;font-size:11px}"; 103 104 style +=".selectThisYear a, .selectThisMonth a{text-decoration:none; margin:0 2px; color:#000; font-weight:bold}"; 105 106 style +=".calender .LastMonth, .calender .NextMonth{ text-decoration:none; color:#000; font-size:18px; font-weight:bold; line-height:16px;}"; 107 108 style +=".calender .LastMonth { float:left;}"; 109 110 style +=".calender .NextMonth { float:right;}"; 111 112 style +=".calenderBody {clear:both}"; 113 114 style +=".calenderTitle {text-align:center;height:20px; line-height:20px; clear:both}"; 115 116 style +=".today { background-color:#ffffaa;border:1px solid #f60; padding:2px}"; 117 118 style +=".today a { color:#f30; }"; 119 120 style +=".calenderBottom {clear:both; border-top:1px solid #ddd; padding: 3px 0; text-align:left;line-height:20px}"; 121 122 style +=".calenderBottom a {text-decoration:none; margin:2px !important; font-weight:bold; color:#000}"; 123 124 style +=".calenderBottom a.closeCalender{float:right;}"; 125 126 style +=".closeCalenderBox {float:right; border:1px solid #000; background:#fff; font-size:9px; width:11px; height:11px; line-height:11px; text-align:center;overflow:hidden; font-weight:normal !important}"; 127 128 style +="</style>"; 129 130 var now; 131 132 if (typeof(arguments[0])=="string"){ 133 134 selectDate = arguments[0].split("-"); 135 136 var year = selectDate[0]; 137 138 var month = parseInt(selectDate[1])-1+""; 139 140 var date = selectDate[2]; 141 142 now = new Date(year,month,date); 143 144 }else if (typeof(arguments[0])=="object"){ 145 146 now = arguments[0]; 147 148 } 149 150 151 152 var lastMonthEndDate = HS_DateAdd("d","-1",now.getFullYear()+"-"+now.getMonth()+"-01").getDate(); 153 154 var lastMonthDate = WeekDay(now.getFullYear()+"-"+now.getMonth()+"-01"); 155 156 var thisMonthLastDate = HS_DateAdd("d","-1",now.getFullYear()+"-"+(parseInt(now.getMonth())+1).toString()+"-01"); 157 158 var thisMonthEndDate = thisMonthLastDate.getDate(); 159 160 var thisMonthEndDay = thisMonthLastDate.getDay(); 161 162 var todayObj = new Date(); 163 164 today = todayObj.getFullYear()+"-"+todayObj.getMonth()+"-"+todayObj.getDate(); 165 166 for (i=0; i<lastMonthDate; i++){ // Last Month‘s Date 167 168 lis = "<li><a href=javascript:void(0) onclick=‘_selectThisDay(this)‘ title=‘"+now.getFullYear()+"-"+(parseInt(now.getMonth()))+"-"+lastMonthEndDate+"‘>"+lastMonthEndDate+"</a></li>" + lis; 169 170 lastMonthEndDate--; 171 172 } 173 174 for (i=1; i<=thisMonthEndDate; i++){ // Current Month‘s Date 175 176 177 178 if(today == now.getFullYear()+"-"+now.getMonth()+"-"+i){ 179 180 var todayString = now.getFullYear()+"-"+(parseInt(now.getMonth())+1).toString()+"-"+i; 181 182 lis += "<li><a href=javascript:void(0) class=‘today‘ onclick=‘_selectThisDay(this)‘ title=‘"+now.getFullYear()+"-"+(parseInt(now.getMonth())+1)+"-"+i+"‘>"+i+"</a></li>"; 183 184 185 186 }else{ 187 188 lis += "<li><a href=javascript:void(0) onclick=‘_selectThisDay(this)‘ title=‘"+now.getFullYear()+"-"+(parseInt(now.getMonth())+1)+"-"+i+"‘>"+i+"</a></li>"; 189 190 191 192 } 193 194 } 195 196 var j=1; 197 198 for (i=thisMonthEndDay; i<6; i++){ // Next Month‘s Date 199 200 lis += "<li><a href=javascript:void(0) onclick=‘_selectThisDay(this)‘ title=‘"+now.getFullYear()+"-"+(parseInt(now.getMonth())+2)+"-"+j+"‘>"+j+"</a></li>"; 201 202 j++; 203 204 } 205 206 lis += style; 207 208 var CalenderTitle = "<a href=‘javascript:void(0)‘ class=‘NextMonth‘ onclick=HS_calender(HS_DateAdd(‘m‘,1,‘"+now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate()+"‘),this) title=‘Next Month‘>?</a>"; 209 210 CalenderTitle += "<a href=‘javascript:void(0)‘ class=‘LastMonth‘ onclick=HS_calender(HS_DateAdd(‘m‘,-1,‘"+now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate()+"‘),this) title=‘Previous Month‘>?</a>"; 211 212 CalenderTitle += "<span class=‘selectThisYear‘><a href=‘javascript:void(0)‘ onclick=‘CalenderselectYear(this)‘ title=‘Click here to select other year‘ >"+now.getFullYear()+"</a></span>年<span class=‘selectThisMonth‘><a href=‘javascript:void(0)‘ onclick=‘CalenderselectMonth(this)‘ title=‘Click here to select other month‘>"+(parseInt(now.getMonth())+1).toString()+"</a></span>月"; 213 214 if (arguments.length>1){ 215 216 arguments[1].parentNode.parentNode.getElementsByTagName("ul")[1].innerHTML = lis; 217 218 arguments[1].parentNode.innerHTML = CalenderTitle; 219 220 }else{ 221 222 var CalenderBox = style+"<div class=‘calender‘><div class=‘calenderTitle‘>"+CalenderTitle+"</div><div class=‘calenderBody‘><ul class=‘day‘><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul><ul class=‘date‘ id=‘thisMonthDate‘>"+lis+"</ul></div><div class=‘calenderBottom‘><a href=‘javascript:void(0)‘ class=‘closeCalender‘ onclick=‘closeCalender(this)‘>×</a><span><span><a href=javascript:void(0) onclick=‘_selectThisDay(this)‘ title=‘"+todayString+"‘>Today</a></span></span></div></div>"; 223 224 return CalenderBox; 225 226 } 227 228 } 229 230 function _selectThisDay(d){ 231 232 var boxObj = d.parentNode.parentNode.parentNode.parentNode.parentNode; 233 234 235 236 var title = d.title; 237 238 var res = title.split(‘-‘); 239 240 if(res[1] < 10){ 241 242 res[1] = ‘0‘+res[1]; 243 244 } 245 246 if(res[2] < 10){ 247 248 res[2] = ‘0‘+res[2]; 249 250 } 251 252 boxObj.targetObj.value = res[0]+res[1]+res[2]; 253 254 boxObj.parentNode.removeChild(boxObj); 255 256 } 257 258 function closeCalender(d){ 259 260 var boxObj = d.parentNode.parentNode.parentNode; 261 262 boxObj.parentNode.removeChild(boxObj); 263 264 } 265 266 function CalenderselectYear(obj){ 267 268 var opt = ""; 269 270 var thisYear = obj.innerHTML; 271 272 for (i=1970; i<=2020; i++){ 273 274 if (i==thisYear){ 275 276 opt += "<option value="+i+" selected>"+i+"</option>"; 277 278 }else{ 279 280 opt += "<option value="+i+">"+i+"</option>"; 281 282 } 283 284 } 285 286 opt = "<select onblur=‘selectThisYear(this)‘ onchange=‘selectThisYear(this)‘ style=‘font-size:11px‘>"+opt+"</select>"; 287 288 obj.parentNode.innerHTML = opt; 289 290 } 291 292 function selectThisYear(obj){ 293 294 HS_calender(obj.value+"-"+obj.parentNode.parentNode.getElementsByTagName("span")[1].getElementsByTagName("a")[0].innerHTML+"-1",obj.parentNode); 295 296 } 297 298 function CalenderselectMonth(obj){ 299 300 var opt = ""; 301 302 var thisMonth = obj.innerHTML; 303 304 for (i=1; i<=12; i++){ 305 306 if (i==thisMonth){ 307 308 opt += "<option value="+i+" selected>"+i+"</option>"; 309 310 }else{ 311 312 opt += "<option value="+i+">"+i+"</option>"; 313 314 } 315 316 } 317 318 opt = "<select onblur=‘selectThisMonth(this)‘ onchange=‘selectThisMonth(this)‘ style=‘font-size:11px‘>"+opt+"</select>"; 319 320 obj.parentNode.innerHTML = opt; 321 322 } 323 324 function selectThisMonth(obj){ 325 326 HS_calender(obj.parentNode.parentNode.getElementsByTagName("span")[0].getElementsByTagName("a")[0].innerHTML+"-"+obj.value+"-1",obj.parentNode); 327 328 } 329 330 function HS_setDate(inputObj){ 331 332 var calenderObj = document.createElement("span"); 333 334 calenderObj.innerHTML = HS_calender(new Date()); 335 336 calenderObj.style.position = "absolute"; 337 338 calenderObj.style.zIndex = "10009"; 339 340 calenderObj.targetObj = inputObj; 341 342 inputObj.parentNode.insertBefore(calenderObj,inputObj.nextSibling); 343 344 }
评论(0)