/** 2 *本日历选择控件由tiannet根据前人经验完善而得。大部分代码来自meizz的日历控件。 3 *tiannet添加了时间选择功能、select,object标签隐藏功能,还有其它小功能。 4 *使用方法: 5 * (1)只选择日期 <input type="text" name="date" readOnly onClick="setDay(this);"> 6 * (2)选择日期和小时 <input type="text" name="dateh" readOnly onClick="setDayH(this);"> 7 * (3)选择日期和小时及分钟 <input type="text" name="datehm" readOnly onClick="setDayHM(this);"> 8 *设置参数的方法 9 * (1)设置日期分隔符 setDateSplit(strSplit);默认为"-" 10 * (2)设置日期与时间之间的分隔符 setDateTimeSplit(strSplit);默认为" " 11 * (3)设置时间分隔符 setTimeSplit(strSplit);默认为":" 12 * (4)设置(1),(2),(3)中的分隔符 setSplit(strDateSplit,strDateTimeSplit,strTimeSplit); 13 * (5)设置开始和结束年份 setYearPeriod(intDateBeg,intDateEnd) 14 *说明: 15 * 默认返回的日期时间格式如同:2005-02-02 08:08 16 */ 17//------------------ 样式定义 ---------------------------// 18//功能按钮同样样式 19var s_tiannet_turn_base = "height:16px;font-size:9pt;color:white;border:0 solid #CCCCCC;cursor:hand;background-color:#2650A6;"; 20//翻年、月等的按钮 21var s_tiannet_turn = s_tiannet_turn_base;//原来的是: "width:28px;" + s_tiannet_turn_base; 22//关闭、清空等按钮样式 23var s_tiannet_turn2 = "width:22px;" + s_tiannet_turn_base; 24//年选择下拉框 25var s_tiannet_select = "width:64px;display:none;"; 26//月、时、分选择下拉框 27var s_tiannet_select2 = "width:46px;display:none;"; 28//日期选择控件体的样式 29var s_tiannet_body = "width:150;background-color:#2650A6;display:none;z-index:9998;position:absolute;" + 30 "border-left:1 solid #CCCCCC;border-top:1 solid #CCCCCC;border-right:1 solid #999999;border-bottom:1 solid #999999;"; 31//显示日的td的样式 32var s_tiannet_day = "width:21px;height:20px;background-color:#D8F0FC;font-size:10pt;"; 33//字体样式 34var s_tiannet_font = "color:#FFCC00;font-size:9pt;cursor:hand;"; 35//链接的样式 36var s_tiannet_link = "text-decoration:none;font-size:9pt;color:#2650A6;"; 37//横线 38var s_tiannet_line = "border-bottom:1 solid #6699CC"; 39//------------------ 变量定义 ---------------------------// 40var tiannetYearSt = 1950;//可选择的开始年份 41var tiannetYearEnd = 2010;//可选择的结束年份 42var tiannetDateNow = new Date(); 43var tiannetYear = tiannetDateNow.getFullYear(); //定义年的变量的初始值 44var tiannetMonth = tiannetDateNow.getMonth()+1; //定义月的变量的初始值 45var tiannetDay = tiannetDateNow.getDate(); 46var tiannetHour = 8;//tiannetDateNow.getHours(); 47var tiannetMinute = 0;//tiannetDateNow.getMinutes(); 48var tiannetArrDay=new Array(42); //定义写日期的数组 49var tiannetDateSplit = "-"; //日期的分隔符号 50var tiannetDateTimeSplit = " "; //日期与时间之间的分隔符 51var tiannetTimeSplit = ":"; //时间的分隔符号 52var tiannetOutObject; //接收日期时间的对象 53var arrTiannetHide = new Array();//被强制隐藏的标签 54var m_bolShowHour = false;//是否显示小时 55var m_bolShowMinute = false;//是否显示分钟 56 57var m_aMonHead = new Array(12); //定义阳历中每个月的最大天数 58 m_aMonHead[0] = 31; m_aMonHead[1] = 28; m_aMonHead[2] = 31; m_aMonHead[3] = 30; m_aMonHead[4] = 31; m_aMonHead[5] = 30; 59 m_aMonHead[6] = 31; m_aMonHead[7] = 31; m_aMonHead[8] = 30; m_aMonHead[9] = 31; m_aMonHead[10] = 30; m_aMonHead[11] = 31; 60// ---------------------- 用户可调用的函数 -----------------------------// 61//用户主调函数-只选择日期 62function setDay(obj){ 63 tiannetOutObject = obj; 64 //如果标签中有值,则将日期初始化为当前值 65 var strValue = tiannetTrim(tiannetOutObject.value); 66 if( strValue != "" ){ 67 tiannetInitDate(strValue); 68 } 69 tiannetPopCalendar(); 70} 71//用户主调函数-选择日期和小时 72function setDayH(obj){ 73 tiannetOutObject = obj; 74 m_bolShowHour = true; 75 //如果标签中有值,则将日期和小时初始化为当前值 76 var strValue = tiannetTrim(tiannetOutObject.value); 77 if( strValue != "" ){ 78 tiannetInitDate(strValue.substring(0,10)); 79 var hour = strValue.substring(11,13); 80 if( hour < 10 ) tiannetHour = hour.substring(1,2); 81 } 82 tiannetPopCalendar(); 83} 84//用户主调函数-选择日期和小时及分钟 85function setDayHM(obj){ 86 tiannetOutObject = obj; 87 m_bolShowHour = true; 88 m_bolShowMinute = true; 89 //如果标签中有值,则将日期和小时及分钟初始化为当前值 90 var strValue = tiannetTrim(tiannetOutObject.value); 91 if( strValue != "" ){ 92 tiannetInitDate(strValue.substring(0,10)); 93 var time = strValue.substring(11,16); 94 var arr = time.split(tiannetTimeSplit); 95 tiannetHour = arr[0]; 96 tiannetMinute = arr[1]; 97 if( tiannetHour < 10 ) tiannetHour = tiannetHour.substring(1,2); 98 if( tiannetMinute < 10 ) tiannetMinute = tiannetMinute.substring(1,2); 99 } 100 tiannetPopCalendar(); 101} 102//设置开始日期和结束日期 103function setYearPeriod(intDateBeg,intDateEnd){ 104 tiannetYearSt = intDateBeg; 105 tiannetYearEnd = intDateEnd; 106} 107//设置日期分隔符。默认为"-" 108function setDateSplit(strDateSplit){ 109 tiannetDateSplit = strDateSplit; 110} 111//设置日期与时间之间的分隔符。默认为" " 112function setDateTimeSplit(strDateTimeSplit){ 113 tiannetDateTimeSplit = strDateTimeSplit; 114} 115//设置时间分隔符。默认为":" 116function setTimeSplit(strTimeSplit){ 117 tiannetTimeSplit = strTimeSplit; 118} 119//设置分隔符 120function setSplit(strDateSplit,strDateTimeSplit,strTimeSplit){ 121 tiannetDateSplit(strDateSplit); 122 tiannetDateTimeSplit(strDateTimeSplit); 123 tiannetTimeSplit(strTimeSplit); 124} 125//设置默认的日期。格式为:YYYY-MM-DD 126function setDefaultDate(strDate){ 127 tiannetYear = strDate.substring(0,4); 128 tiannetMonth = strDate.substring(5,7); 129 tiannetDay = strDate.substring(8,10); 130} 131//设置默认的时间。格式为:HH24:MI 132function setDefaultTime(strTime){ 133 tiannetHour = strTime.substring(0,2); 134 tiannetMinute = strTime.substring(3,5); 135} 136// ---------------------- end 用户可调用的函数 -----------------------------// 137//------------------ begin 页面显示部分 ---------------------------// 138var weekName = new Array("日","一","二","三","四","五","六"); 139document.write('<div id="divTiannetDate" style="'+s_tiannet_body+'" style="本日历选择控件由tiannet根据前人经验完善而成!">'); 140document.write('<div align="center" id="divTiannetDateText" Author="tiannet" style="padding-top:2px;">'); 141document.write('<span id="tiannetYearHead" Author="tiannet" style="'+s_tiannet_font+'" '+ 142 'onclick="spanYearCEvent();"> 年</span>'); 143document.write('<select id="selTianYear" style="'+s_tiannet_select+'" Author="tiannet" '+ 144 ' onChange="tiannetYear=this.value;tiannetSetDay(tiannetYear,tiannetMonth);document.all.tiannetYearHead.style.display=\'\';'+ 145 'this.style.display=\'none\';">'); 146for(var i=tiannetYearSt;i <= tiannetYearEnd;i ++){ 147 document.writeln('<option value="' + i + '">' + i + '年</option>'); 148} 149document.write('</select>'); 150document.write('<span id="tiannetMonthHead" Author="tiannet" style="'+s_tiannet_font+'" '+ 151 'onclick="spanMonthCEvent();"> 月</span>'); 152document.write('<select id="selTianMonth" style="'+s_tiannet_select2+'" Author="tiannet" '+ 153 'onChange="tiannetMonth=this.value;tiannetSetDay(tiannetYear,tiannetMonth);document.all.tiannetMonthHead.style.display=\'\';'+ 154 'this.style.display=\'none\';">'); 155for(var i=1;i <= 12;i ++){ 156 document.writeln('<option value="' + i + '">' + i + '月</option>'); 157} 158document.write('</select>'); 159//document.write('</div>'); 160//document.write('<div align="center" id="divTiannetTimeText" Author="tiannet">'); 161document.write('<span id="tiannetHourHead" Author="tiannet" style="'+s_tiannet_font+'display:none;" '+ 162 'onclick="spanHourCEvent();"> 时</span>'); 163document.write('<select id="selTianHour" style="'+s_tiannet_select2+'display:none;" Author="tiannet" '+ 164 ' onChange="tiannetHour=this.value;tiannetWriteHead();document.all.tiannetHourHead.style.display=\'\';' + 165 'this.style.display=\'none\';">'); 166for(var i=0;i <= 23;i ++){ 167 document.writeln('<option value="' + i + '">' + i + '时</option>'); 168} 169document.write('</select>'); 170document.write('<span id="tiannetMinuteHead" Author="tiannet" style="'+s_tiannet_font+'display:none;" '+ 171 'onclick="spanMinuteCEvent();"> 分</span>'); 172document.write('<select id="selTianMinute" style="'+s_tiannet_select2+'display:none;" Author="tiannet" '+ 173 ' onChange="tiannetMinute=this.value;tiannetWriteHead();document.all.tiannetMinuteHead.style.display=\'\';'+ 174 'this.style.display=\'none\';">'); 175for(var i=0;i <= 59;i ++){ 176 document.writeln('<option value="' + i + '">' + i + '分</option>'); 177} 178document.write('</select>'); 179document.write('</div>'); 180//输出一条横线 181document.write('<div style="'+s_tiannet_line+'"></div>'); 182document.write('<div align="center" id="divTiannetTurn" style="border:0;" Author="tiannet">'); 183document.write('<input type="button" style="'+s_tiannet_turn+'" value="▲" title="上一年" onClick="tiannetPrevYear();">');//◄►▲▼ 184document.write('<font style="'+s_tiannet_turn+'">年</font>'); 185document.write('<input type="button" style="'+s_tiannet_turn+'" value="▼" title="下一年" onClick="tiannetNextYear();"> '); 186document.write('<input type="button" style="'+s_tiannet_turn+'" value="▲" title="上一月" onClick="tiannetPrevMonth();">'); 187document.write('<font style="'+s_tiannet_turn+'">月</font>'); 188document.write('<input type="button" style="'+s_tiannet_turn+'" value="▼" title="下一月" onClick="tiannetNextMonth();">'); 189 190document.write('</div>'); 191//输出一条横线 192document.write('<div style="'+s_tiannet_line+'"></div>'); 193document.write('<table border=0 cellspacing=0 cellpadding=0 bgcolor=white onselectstart="return false">'); 194document.write(' <tr style="background-color:#2650A6;font-size:10pt;color:white;height:22px;" Author="tiannet">'); 195for(var i =0;i < weekName.length;i ++){ 196 //输出星期 197 document.write('<td width="21" align="center" Author="tiannet">' + weekName[i] + '</td>'); 198} 199document.write(' </tr>'); 200document.write('</table>'); 201//输出天的选择 202document.write('<table border=0 cellspacing=1 cellpadding=0 bgcolor=white onselectstart="return false">'); 203var n = 0; 204for (var i=0;i<5;i++) { 205 document.write (' <tr align=center id="trTiannetDay' + i + '" >'); 206 for (var j=0;j<7;j++){ 207 document.write('<td align="center" id="tdTiannetDay' + n + '" '+ 208 'onClick="tiannetDay=this.innerText;tiannetSetValue(true);" ' 209 +' style="' + s_tiannet_day + '"> </td>'); 210 n ++; 211 } 212 document.write (' </tr>'); 213} 214document.write (' <tr align=center id="trTiannetDay5" >'); 215document.write('<td align="center" id="tdTiannetDay35" onClick="tiannetDay=this.innerText;tiannetSetValue(true);" ' 216 +' style="' + s_tiannet_day + '"> </td>'); 217document.write('<td align="center" id="tdTiannetDay36" onClick="tiannetDay=this.innerText;tiannetSetValue(true);" ' 218 +' style="' + s_tiannet_day + '"> </td>'); 219document.write('<td align="right" colspan="5"><a href="javascript:tiannetClear();" style="' + s_tiannet_link + '">清空</a>'+ 220 ' <a href="javascript:tiannetHideControl();" style="' + s_tiannet_link + '">关闭</a>' + 221 ' <a href="javascript:tiannetSetValue(true);" style="' + s_tiannet_link + '">确定</a> ' + 222 '</td>'); 223document.write (' </tr>'); 224document.write('</table>'); 225document.write('</div>'); 226//------------------ end 页面显示部分 ---------------------------// 227//------------------ 显示日期时间的span标签响应事件 ---------------------------// 228//单击年份span标签响应 229function spanYearCEvent(){ 230 hideElementsById(new Array("selTianYear","tiannetMonthHead"),false); 231 if(m_bolShowHour) hideElementsById(new Array("tiannetHourHead"),false); 232 if(m_bolShowMinute) hideElementsById(new Array("tiannetMinuteHead"),false); 233 hideElementsById(new Array("tiannetYearHead","selTianMonth","selTianHour","selTianMinute"),true); 234} 235//单击月份span标签响应 236function spanMonthCEvent(){ 237 hideElementsById(new Array("selTianMonth","tiannetYearHead"),false); 238 if(m_bolShowHour) hideElementsById(new Array("tiannetHourHead"),false); 239 if(m_bolShowMinute) hideElementsById(new Array("tiannetMinuteHead"),false); 240 hideElementsById(new Array("tiannetMonthHead","selTianYear","selTianHour","selTianMinute"),true); 241} 242//单击小时span标签响应 243function spanHourCEvent(){ 244 hideElementsById(new Array("tiannetYearHead","tiannetMonthHead"),false); 245 if(m_bolShowHour) hideElementsById(new Array("selTianHour"),false); 246 if(m_bolShowMinute) hideElementsById(new Array("tiannetMinuteHead"),false); 247 hideElementsById(new Array("tiannetHourHead","selTianYear","selTianMonth","selTianMinute"),true); 248} 249//单击分钟span标签响应 250function spanMinuteCEvent(){ 251 hideElementsById(new Array("tiannetYearHead","tiannetMonthHead"),false); 252 if(m_bolShowHour) hideElementsById(new Array("tiannetHourHead"),false); 253 if(m_bolShowMinute) hideElementsById(new Array("selTianMinute"),false); 254 hideElementsById(new Array("tiannetMinuteHead","selTianYear","selTianMonth","selTianHour"),true); 255} 256//根据标签id隐藏或显示标签 257function hideElementsById(arrId,bolHide){ 258 var strDisplay = ""; 259 if(bolHide) strDisplay = "none"; 260 for(var i = 0;i < arrId.length;i ++){ 261 var obj = document.getElementById(arrId[i]); 262 obj.style.display = strDisplay; 263 } 264} 265//------------------ end 显示日期时间的span标签响应事件 ---------------------------// 266//判断某年是否为闰年 267function isPinYear(year){ 268 var bolRet = false; 269 if (0==year%4&&((year%100!=0)||(year%400==0))) { 270 bolRet = true; 271 } 272 return bolRet; 273} 274//得到一个月的天数,闰年为29天 275function getMonthCount(year,month){ 276 var c=m_aMonHead[month-1]; 277 if((month==2)&&isPinYear(year)) c++; 278 return c; 279} 280//重新设置当前的日。主要是防止在翻年、翻月时,当前日大于当月的最大日 281function setRealDayCount() { 282 if( tiannetDay > getMonthCount(tiannetYear,tiannetMonth) ) { 283 //如果当前的日大于当月的最大日,则取当月最大日 284 tiannetDay = getMonthCount(tiannetYear,tiannetMonth); 285 } 286} 287//在个位数前加零 288function addZero(value){ 289 if(value < 10 ){ 290 value = "0" + value; 291 } 292 return value; 293} 294//取出空格 295function tiannetTrim(str) { 296 return str.replace(/(^\s*)|(\s*$)/g,""); 297} 298//为select创建一个option 299function createOption(objSelect,value,text){ 300 var option = document.createElement("OPTION"); 301 option.value = value; 302 option.text = text; 303 objSelect.options.add(option); 304} 305//往前翻 Year 306function tiannetPrevYear() { 307 if(tiannetYear > 999 && tiannetYear <10000){tiannetYear--;} 308 else{alert("年份超出范围(1000-9999)!");} 309 tiannetSetDay(tiannetYear,tiannetMonth); 310 //如果年份小于允许的最小年份,则创建对应的option 311 if( tiannetYear < tiannetYearSt ) { 312 tiannetYearSt = tiannetYear; 313 createOption(document.all.selTianYear,tiannetYear,tiannetYear + "年"); 314 } 315 checkSelect(document.all.selTianYear,tiannetYear); 316 tiannetWriteHead(); 317} 318//往后翻 Year 319function tiannetNextYear() { 320 if(tiannetYear > 999 && tiannetYear <10000){tiannetYear++;} 321 else{alert("年份超出范围(1000-9999)!");return;} 322 tiannetSetDay(tiannetYear,tiannetMonth); 323 //如果年份超过允许的最大年份,则创建对应的option 324 if( tiannetYear > tiannetYearEnd ) { 325 tiannetYearEnd = tiannetYear; 326 createOption(document.all.selTianYear,tiannetYear,tiannetYear + "年"); 327 } 328 checkSelect(document.all.selTianYear,tiannetYear); 329 tiannetWriteHead(); 330} 331//选择今天 332function tiannetToday() { 333 tiannetYear = tiannetDateNow.getFullYear(); 334 tiannetMonth = tiannetDateNow.getMonth()+1; 335 tiannetDay = tiannetDateNow.getDate(); 336 tiannetSetValue(true); 337 //tiannetSetDay(tiannetYear,tiannetMonth); 338 //selectObject(); 339} 340//往前翻月份 341function tiannetPrevMonth() { 342 if(tiannetMonth>1){tiannetMonth--}else{tiannetYear--;tiannetMonth=12;} 343 tiannetSetDay(tiannetYear,tiannetMonth); 344 checkSelect(document.all.selTianMonth,tiannetMonth); 345 tiannetWriteHead(); 346} 347//往后翻月份 348function tiannetNextMonth() { 349 if(tiannetMonth==12){tiannetYear++;tiannetMonth=1}else{tiannetMonth++} 350 tiannetSetDay(tiannetYear,tiannetMonth); 351 checkSelect(document.all.selTianMonth,tiannetMonth); 352 tiannetWriteHead(); 353} 354//向span标签中写入年、月、时、分等数据 355function tiannetWriteHead(){ 356 document.all.tiannetYearHead.innerText = tiannetYear + "年"; 357 document.all.tiannetMonthHead.innerText = tiannetMonth + "月"; 358 if( m_bolShowHour ) document.all.tiannetHourHead.innerText = " "+tiannetHour + "时"; 359 if( m_bolShowMinute ) document.all.tiannetMinuteHead.innerText = tiannetMinute + "分"; 360 tiannetSetValue(false);//给文本框赋值,但不隐藏本控件 361} 362//设置显示天 363function tiannetSetDay(yy,mm) { 364 365 setRealDayCount();//设置当月真实的日 366 tiannetWriteHead(); 367 var strDateFont1 = "", strDateFont2 = "" //处理日期显示的风格 368 for (var i = 0; i < 37; i++){tiannetArrDay[i]=""}; //将显示框的内容全部清空 369 var day1 = 1; 370 var firstday = new Date(yy,mm-1,1).getDay(); //某月第一天的星期几 371 for (var i = firstday; day1 < getMonthCount(yy,mm)+1; i++){ 372 tiannetArrDay[i]=day1;day1++; 373 } 374 //如果用于显示日的最后一行的第一个单元格的值为空,则隐藏整行。 375 //if(tiannetArrDay[35] == ""){ 376 // document.all.trTiannetDay5.style.display = "none"; 377 //} else { 378 // document.all.trTiannetDay5.style.display = ""; 379 //} 380 for (var i = 0; i < 37; i++){ 381 var da = eval("document.all.tdTiannetDay"+i) //书写新的一个月的日期星期排列 382 if (tiannetArrDay[i]!="") { 383 //判断是否为周末,如果是周末,则改为红色字体 384 if(i % 7 == 0 || (i+1) % 7 == 0){ 385 strDateFont1 = "<font color=#f0000>" 386 strDateFont2 = "</font>" 387 } else { 388 strDateFont1 = ""; 389 strDateFont2 = "" 390 } 391 da.innerHTML = strDateFont1 + tiannetArrDay[i] + strDateFont2; 392 //如果是当前选择的天,则改变颜色 393 if(tiannetArrDay[i] == tiannetDay ) { 394 da.style.backgroundColor = "#CCCCCC"; 395 } else { 396 da.style.backgroundColor = "#EFEFEF"; 397 } 398 da.style.cursor="hand" 399 } else { 400 da.innerHTML="";da.style.backgroundColor="";da.style.cursor="default" 401 } 402 }//end for 403 tiannetSetValue(false);//给文本框赋值,但不隐藏本控件 404}//end function tiannetSetDay 405//根据option的值选中option 406function checkSelect(objSelect,selectValue) { 407 var count = parseInt(objSelect.length); 408 if( selectValue < 10 && selectValue.toString().length == 2) { 409 selectValue = selectValue.substring(1,2); 410 } 411 for(var i = 0;i < count;i ++){ 412 if(objSelect.options[i].value == selectValue){ 413 objSelect.selectedIndex = i; 414 break; 415 } 416 }//for 417} 418//选中年、月、时、分等下拉框 419function selectObject(){ 420 //如果年份小于允许的最小年份,则创建对应的option 421 if( tiannetYear < tiannetYearSt ) { 422 for( var i = tiannetYear;i < tiannetYearSt;i ++ ){ 423 createOption(document.all.selTianYear,i,i + "年"); 424 } 425 tiannetYearSt = tiannetYear; 426 } 427 //如果年份超过允许的最大年份,则创建对应的option 428 if( tiannetYear > tiannetYearEnd ) { 429 for( var i = tiannetYearEnd+1;i <= tiannetYear;i ++ ){ 430 createOption(document.all.selTianYear,i,i + "年"); 431 } 432 tiannetYearEnd = tiannetYear; 433 } 434 checkSelect(document.all.selTianYear,tiannetYear); 435 checkSelect(document.all.selTianMonth,tiannetMonth); 436 if( m_bolShowHour ) checkSelect(document.all.selTianHour,tiannetHour); 437 if( m_bolShowMinute ) checkSelect(document.all.selTianMinute,tiannetMinute); 438} 439//给接受日期时间的控件赋值 440//参数bolHideControl - 是否隐藏控件 441function tiannetSetValue(bolHideControl){ 442 var value = ""; 443 if( !tiannetDay || tiannetDay == "" ){ 444 tiannetOutObject.value = value; 445 return; 446 } 447 var mm = tiannetMonth; 448 var day = tiannetDay; 449 if( mm < 10 && mm.toString().length == 1) mm = "0" + mm; 450 if( day < 10 && day.toString().length == 1) day = "0" + day; 451 value = tiannetYear + tiannetDateSplit + mm + tiannetDateSplit + day; 452 if( m_bolShowHour ){ 453 var hour = tiannetHour; 454 if( hour < 10 && hour.toString().length == 1 ) hour = "0" + hour; 455 value += tiannetDateTimeSplit + hour; 456 } 457 if( m_bolShowMinute ){ 458 var minute = tiannetMinute; 459 if( minute < 10 && minute.toString().length == 1 ) minute = "0" + minute; 460 value += tiannetTimeSplit + minute; 461 } 462 tiannetOutObject.value = value; 463 //document.all.divTiannetDate.style.display = "none"; 464 if( bolHideControl ) { 465 tiannetHideControl(); 466 } 467} 468//是否显示时间 469function showTime(){ 470 if( !m_bolShowHour && m_bolShowMinute){ 471 alert("如果要选择分钟,则必须可以选择小时!"); 472 return; 473 } 474 hideElementsById(new Array("tiannetHourHead","selTianHour","tiannetMinuteHead","selTianMinute"),true); 475 if( m_bolShowHour ){ 476 //显示小时 477 hideElementsById(new Array("tiannetHourHead"),false); 478 } 479 if( m_bolShowMinute ){ 480 //显示分钟 481 hideElementsById(new Array("tiannetMinuteHead"),false); 482 } 483} 484//弹出显示日历选择控件,以让用户选择 485function tiannetPopCalendar(){ 486 //隐藏下拉框,显示相对应的head 487 hideElementsById(new Array("selTianYear","selTianMonth","selTianHour","selTianMinute"),true); 488 hideElementsById(new Array("tiannetYearHead","tiannetMonthHead","tiannetHourHead","tiannetMinuteHead"),false); 489 tiannetSetDay(tiannetYear,tiannetMonth); 490 tiannetWriteHead(); 491 showTime(); 492 var dads = document.all.divTiannetDate.style; 493 var iX, iY; 494 495 var h = document.all.divTiannetDate.offsetHeight; 496 var w = document.all.divTiannetDate.offsetWidth; 497 //计算left 498 if (window.event.x + h > document.body.offsetWidth - 10 ) 499 iX = window.event.x - h - 5 ; 500 else 501 iX = window.event.x + 5; 502 if (iX <0) 503 iX=0; 504 //计算top 505 iY = window.event.y; 506 if (window.event.y + w > document.body.offsetHeight - 10 ) 507 iY = document.body.scrollTop + document.body.offsetHeight - w - 5 ; 508 else 509 iY = document.body.scrollTop +window.event.y + 5; 510 if (iY <0) 511 iY=0; 512 dads.left = iX; 513 dads.top = iY; 514 tiannetShowControl(); 515 selectObject(); 516} 517//隐藏日历控件(同时显示被强制隐藏的标签) 518function tiannetHideControl(){ 519 document.all.divTiannetDate.style.display = "none"; 520 tiannetShowObject(); 521 arrTiannetHide = new Array();//将被隐藏的标签对象清空 522} 523//显示日历控件(同时隐藏会遮挡的标签) 524function tiannetShowControl(){ 525 document.all.divTiannetDate.style.display = ""; 526 tiannetHideObject("SELECT"); 527 tiannetHideObject("OBJECT"); 528} 529//根据标签名称隐藏标签。如会遮住控件的select,object 530function tiannetHideObject(strTagName) { 531 532 x = document.all.divTiannetDate.offsetLeft; 533 y = document.all.divTiannetDate.offsetTop; 534 h = document.all.divTiannetDate.offsetHeight; 535 w = document.all.divTiannetDate.offsetWidth; 536 537 for (var i = 0; i < document.all.tags(strTagName).length; i++) 538 { 539 540 var obj = document.all.tags(strTagName)[i]; 541 if (! obj || ! obj.offsetParent) 542 continue; 543 // 获取元素对于BODY标记的相对坐标 544 var objLeft = obj.offsetLeft; 545 var objTop = obj.offsetTop; 546 var objHeight = obj.offsetHeight; 547 var objWidth = obj.offsetWidth; 548 var objParent = obj.offsetParent; 549 550 while (objParent.tagName.toUpperCase() != "BODY"){ 551 objLeft += objParent.offsetLeft; 552 objTop += objParent.offsetTop; 553 objParent = objParent.offsetParent; 554 } 555 //alert("控件左端:" + x + "select左端" + (objLeft + objWidth) + "控件底部:" + (y+h) + "select高:" + objTop); 556 557 var bolHide = true; 558 if( obj.style.display == "none" || obj.style.visibility == "hidden" || obj.getAttribute("Author") == "tiannet" ){ 559 //如果标签本身就是隐藏的,则不需要再隐藏。如果是控件中的下拉框,也不用隐藏。 560 bolHide = false; 561 } 562 if( ( (objLeft + objWidth) > x && (y + h + 20) > objTop && (objTop+objHeight) > y && objLeft < (x+w) ) && bolHide ){ 563 //arrTiannetHide.push(obj);//记录被隐藏的标签对象 564 arrTiannetHide[arrTiannetHide.length] = obj; 565 obj.style.visibility = "hidden"; 566 } 567 568 569 } 570} 571//显示被隐藏的标签 572function tiannetShowObject(){ 573 for(var i = 0;i < arrTiannetHide.length;i ++){ 574 //alert(arrTiannetHide[i]); 575 arrTiannetHide[i].style.visibility = ""; 576 } 577} 578//初始化日期。 579function tiannetInitDate(strDate){ 580 var arr = strDate.split(tiannetDateSplit); 581 tiannetYear = arr[0]; 582 tiannetMonth = arr[1]; 583 tiannetDay = arr[2]; 584} 585//清空 586function tiannetClear(){ 587 tiannetOutObject.value = ""; 588 tiannetHideControl(); 589} 590 //任意点击时关闭该控件 591function document.onclick(){ 592 with(window.event.srcElement){ 593 if (tagName != "INPUT" && getAttribute("Author") != "tiannet") 594 tiannetHideControl(); 595 } 596} 597//按ESC键关闭该控件 598function document.onkeypress(){ 599 if( event.keyCode == 27 ){ 600 tiannetHideControl(); 601 } 602}