鑒於網上找到的幾個日期選擇程序有些問題,遂著手重寫一個程序,大部分還是借鑒前人的代碼,添加了時間選擇功能,隱藏會遮擋控件的標簽select,object。
開始本想使用window.createPopup()來彈出日歷的選擇,這樣就可以跨過任何標簽。
不過做到中途發現用createPopup窗口實現理論上是行不通的:
一是因為不在窗體裡單擊任何地方都會關閉窗口,而當用下拉框選擇年份時,很有可能會單擊到窗體外的地方,當然這個可以自己寫select來避免,但是比較麻煩;
二是窗體的寬度和高度只能在彈出時設置,而顯然,在選擇不同年月時,控件高度是要發生改變的。
鑒於上面的原因,還是決定采用普通的處理方法。
Calendar.js:
復制代碼 代碼如下:
/**
*本日歷選擇控件由tiannet根據前人經驗完善而得。大部分代碼來自meizz的日歷控件。
*tiannet添加了時間選擇功能、select,object標簽隱藏功能,還有其它小功能。
*使用方法:
* (1)只選擇日期 <input type="text" name="date" readOnly onClick="setDay(this);">
* (2)選擇日期和小時 <input type="text" name="dateh" readOnly onClick="setDayH(this);">
* (3)選擇日期和小時及分鐘 <input type="text" name="datehm" readOnly onClick="setDayHM(this);">
*設置參數的方法
* (1)設置日期分隔符 setDateSplit(strSplit);默認為"-"
* (2)設置日期與時間之間的分隔符 setDateTimeSplit(strSplit);默認為" "
* (3)設置時間分隔符 setTimeSplit(strSplit);默認為":"
* (4)設置(1),(2),(3)中的分隔符 setSplit(strDateSplit,strDateTimeSplit,strTimeSplit);
* (5)設置開始和結束年份 setYearPeriod(intDateBeg,intDateEnd)
*說明:
* 默認返回的日期時間格式如同:2005-02-02 08:08
*/
//------------------ 樣式定義 ---------------------------//
//功能按鈕同樣樣式
var s_tiannet_turn_base = "height:16px;font-size:9pt;color:white;border:0 solid #CCCCCC;cursor:hand;background-color:#2650A6;";
//翻年、月等的按鈕
var s_tiannet_turn = "width:28px;" + s_tiannet_turn_base;
//關閉、清空等按鈕樣式
var s_tiannet_turn2 = "width:22px;" + s_tiannet_turn_base;
//年選擇下拉框
var s_tiannet_select = "width:64px;display:none;";
//月、時、分選擇下拉框
var s_tiannet_select2 = "width:46px;display:none;";
//日期選擇控件體的樣式
var s_tiannet_body = "width:150;background-color:#2650A6;display:none;z-index:9998;position:absolute;" +
"border-left:1 solid #CCCCCC;border-top:1 solid #CCCCCC;border-right:1 solid #999999;border-bottom:1 solid #999999;";
//顯示日的td的樣式
var s_tiannet_day = "width:21px;height:20px;background-color:#D8F0FC;font-size:10pt;";
//字體樣式
var s_tiannet_font = "color:#FFCC00;font-size:9pt;cursor:hand;";
//鏈接的樣式
var s_tiannet_link = "text-decoration:none;font-size:9pt;color:#2650A6;";
//橫線
var s_tiannet_line = "border-bottom:1 solid #6699CC";
//------------------ 變量定義 ---------------------------//
var tiannetYearSt = 1950;//可選擇的開始年份
var tiannetYearEnd = 2010;//可選擇的結束年份
var tiannetDateNow = new Date();
var tiannetYear = tiannetDateNow.getFullYear(); //定義年的變量的初始值
var tiannetMonth = tiannetDateNow.getMonth()+1; //定義月的變量的初始值
var tiannetDay = tiannetDateNow.getDate();
var tiannetHour = 8;//tiannetDateNow.getHours();
var tiannetMinute = 0;//tiannetDateNow.getMinutes();
var tiannetArrDay=new Array(42); //定義寫日期的數組
var tiannetDateSplit = "-"; //日期的分隔符號
var tiannetDateTimeSplit = " "; //日期與時間之間的分隔符
var tiannetTimeSplit = ":"; //時間的分隔符號
var tiannetOutObject; //接收日期時間的對象
var arrTiannetHide = new Array();//被強制隱藏的標簽
var m_bolShowHour = false;//是否顯示小時
var m_bolShowMinute = false;//是否顯示分鐘
var m_aMonHead = new Array(12); //定義陽歷中每個月的最大天數
m_aMonHead[0] = 31; m_aMonHead[1] = 28; m_aMonHead[2] = 31; m_aMonHead[3] = 30; m_aMonHead[4] = 31; m_aMonHead[5] = 30;
m_aMonHead[6] = 31; m_aMonHead[7] = 31; m_aMonHead[8] = 30; m_aMonHead[9] = 31; m_aMonHead[10] = 30; m_aMonHead[11] = 31;
// ---------------------- 用戶可調用的函數 -----------------------------//
//用戶主調函數-只選擇日期
function setDay(obj){
tiannetOutObject = obj;
//如果標簽中有值,則將日期初始化為當前值
var strValue = tiannetTrim(tiannetOutObject.value);
if( strValue != "" ){
tiannetInitDate(strValue);
}
tiannetPopCalendar();
}
//用戶主調函數-選擇日期和小時
function setDayH(obj){
tiannetOutObject = obj;
m_bolShowHour = true;
//如果標簽中有值,則將日期和小時初始化為當前值
var strValue = tiannetTrim(tiannetOutObject.value);
if( strValue != "" ){
tiannetInitDate(strValue.substring(0,10));
var hour = strValue.substring(11,13);
if( hour < 10 ) tiannetHour = hour.substring(1,2);
}
tiannetPopCalendar();
}
//用戶主調函數-選擇日期和小時及分鐘
function setDayHM(obj){
tiannetOutObject = obj;
m_bolShowHour = true;
m_bolShowMinute = true;
//如果標簽中有值,則將日期和小時及分鐘初始化為當前值
var strValue = tiannetTrim(tiannetOutObject.value);
if( strValue != "" ){
tiannetInitDate(strValue.substring(0,10));
var time = strValue.substring(11,16);
var arr = time.split(tiannetTimeSplit);
tiannetHour = arr[0];
tiannetMinute = arr[1];
if( tiannetHour < 10 ) tiannetHour = tiannetHour.substring(1,2);
if( tiannetMinute < 10 ) tiannetMinute = tiannetMinute.substring(1,2);
}
tiannetPopCalendar();
}
//設置開始日期和結束日期
function setYearPeriod(intDateBeg,intDateEnd){
tiannetYearSt = intDateBeg;
tiannetYearEnd = intDateEnd;
}
//設置日期分隔符。默認為"-"
function setDateSplit(strDateSplit){
tiannetDateSplit = strDateSplit;
}
//設置日期與時間之間的分隔符。默認為" "
function setDateTimeSplit(strDateTimeSplit){
tiannetDateTimeSplit = strDateTimeSplit;
}
//設置時間分隔符。默認為":"
function setTimeSplit(strTimeSplit){
tiannetTimeSplit = strTimeSplit;
}
//設置分隔符
function setSplit(strDateSplit,strDateTimeSplit,strTimeSplit){
tiannetDateSplit(strDateSplit);
tiannetDateTimeSplit(strDateTimeSplit);
tiannetTimeSplit(strTimeSplit);
}
//設置默認的日期。格式為:YYYY-MM-DD
function setDefaultDate(strDate){
tiannetYear = strDate.substring(0,4);
tiannetMonth = strDate.substring(5,7);
tiannetDay = strDate.substring(8,10);
}
//設置默認的時間。格式為:HH24:MI
function setDefaultTime(strTime){
tiannetHour = strTime.substring(0,2);
tiannetMinute = strTime.substring(3,5);
}
// ---------------------- end 用戶可調用的函數 -----------------------------//
//------------------ begin 頁面顯示部分 ---------------------------//
var weekName = new Array("日","一","二","三","四","五","六");
document.write('<div id="divTiannetDate" style="'+s_tiannet_body+'" style="本日歷選擇控件由tiannet根據前人經驗完善而成!">');
document.write('<div align="center" id="divTiannetDateText" Author="tiannet" style="padding-top:2px;">');
document.write('<span id="tiannetYearHead" Author="tiannet" style="'+s_tiannet_font+'" '+
'onclick="spanYearCEvent();"> 年</span>');
document.write('<select id="selTianYear" style="'+s_tiannet_select+'" Author="tiannet" '+
' onChange="tiannetYear=this.value;tiannetSetDay(tiannetYear,tiannetMonth);document.all.tiannetYearHead.style.display=\'\';'+
'this.style.display=\'none\';">');
for(var i=tiannetYearSt;i <= tiannetYearEnd;i ++){
document.writeln('<option value="' + i + '">' + i + '年</option>');
}
document.write('</select>');
document.write('<span id="tiannetMonthHead" Author="tiannet" style="'+s_tiannet_font+'" '+
'onclick="spanMonthCEvent();"> 月</span>');
document.write('<select id="selTianMonth" style="'+s_tiannet_select2+'" Author="tiannet" '+
'onChange="tiannetMonth=this.value;tiannetSetDay(tiannetYear,tiannetMonth);document.all.tiannetMonthHead.style.display=\'\';'+
'this.style.display=\'none\';">');
for(var i=1;i <= 12;i ++){
document.writeln('<option value="' + i + '">' + i + '月</option>');
}
document.write('</select>');
//document.write('</div>');
//document.write('<div align="center" id="divTiannetTimeText" Author="tiannet">');
document.write('<span id="tiannetHourHead" Author="tiannet" style="'+s_tiannet_font+'display:none;" '+
'onclick="spanHourCEvent();"> 時</span>');
document.write('<select id="selTianHour" style="'+s_tiannet_select2+'display:none;" Author="tiannet" '+
' onChange="tiannetHour=this.value;tiannetWriteHead();document.all.tiannetHourHead.style.display=\'\';' +
'this.style.display=\'none\';">');
for(var i=0;i <= 23;i ++){
document.writeln('<option value="' + i + '">' + i + '時</option>');
}
document.write('</select>');
document.write('<span id="tiannetMinuteHead" Author="tiannet" style="'+s_tiannet_font+'display:none;" '+
'onclick="spanMinuteCEvent();"> 分</span>');
document.write('<select id="selTianMinute" style="'+s_tiannet_select2+'display:none;" Author="tiannet" '+
' onChange="tiannetMinute=this.value;tiannetWriteHead();document.all.tiannetMinuteHead.style.display=\'\';'+
'this.style.display=\'none\';">');
for(var i=0;i <= 59;i ++){
document.writeln('<option value="' + i + '">' + i + '分</option>');
}
document.write('</select>');
document.write('</div>');
//輸出一條橫線
document.write('<div style="'+s_tiannet_line+'"></div>');
document.write('<div align="center" id="divTiannetTurn" style="border:0;" Author="tiannet">');
document.write('<input type="button" style="'+s_tiannet_turn+'" value="年↑" title="上一年" onClick="tiannetPrevYear();">');
document.write('<input type="button" style="'+s_tiannet_turn+'" value="年↓" title="下一年" onClick="tiannetNextYear();"> ');
document.write('<input type="button" style="'+s_tiannet_turn+'" value="月↑" title="上一月" onClick="tiannetPrevMonth();">');
document.write('<input type="button" style="'+s_tiannet_turn+'" value="月↓" title="下一月" onClick="tiannetNextMonth();">');
document.write('</div>');
//輸出一條橫線
document.write('<div style="'+s_tiannet_line+'"></div>');
document.write('<table border=0 cellspacing=0 cellpadding=0 bgcolor=white onselectstart="return false">');
document.write(' <tr style="background-color:#2650A6;font-size:10pt;color:white;height:22px;" Author="tiannet">');
for(var i =0;i < weekName.length;i ++){
//輸出星期
document.write('<td width="21" align="center" Author="tiannet">' + weekName[i] + '</td>');
}
document.write(' </tr>');
document.write('</table>');
//輸出天的選擇
document.write('<table border=0 cellspacing=1 cellpadding=0 bgcolor=white onselectstart="return false">');
var n = 0;
for (var i=0;i<5;i++) {
document.write (' <tr align=center id="trTiannetDay' + i + '" >');
for (var j=0;j<7;j++){
document.write('<td align="center" id="tdTiannetDay' + n + '" '+
'onClick="tiannetDay=this.innerText;tiannetSetValue(true);" '
+' style="' + s_tiannet_day + '"> </td>');
n ++;
}
document.write (' </tr>');
}
document.write (' <tr align=center id="trTiannetDay5" >');
document.write('<td align="center" id="tdTiannetDay35" onClick="tiannetDay=this.innerText;tiannetSetValue(true);" '
+' style="' + s_tiannet_day + '"> </td>');
document.write('<td align="center" id="tdTiannetDay36" onClick="tiannetDay=this.innerText;tiannetSetValue(true);" '
+' style="' + s_tiannet_day + '"> </td>');
document.write('<td align="right" colspan="5"><a href="javascript:tiannetClear();" style="' + s_tiannet_link + '">清空</a>'+
' <a href="javascript:tiannetHideControl();" style="' + s_tiannet_link + '">關閉</a>' +
' <a href="javascript:tiannetSetValue(true);" style="' + s_tiannet_link + '">確定</a> ' +
'</td>');
document.write (' </tr>');
document.write('</table>');
document.write('</div>');
//------------------ end 頁面顯示部分 ---------------------------//
//------------------ 顯示日期時間的span標簽響應事件 ---------------------------//
//單擊年份span標簽響應
function spanYearCEvent(){
hideElementsById(new Array("selTianYear","tiannetMonthHead"),false);
if(m_bolShowHour) hideElementsById(new Array("tiannetHourHead"),false);
if(m_bolShowMinute) hideElementsById(new Array("tiannetMinuteHead"),false);
hideElementsById(new Array("tiannetYearHead","selTianMonth","selTianHour","selTianMinute"),true);
}
//單擊月份span標簽響應
function spanMonthCEvent(){
hideElementsById(new Array("selTianMonth","tiannetYearHead"),false);
if(m_bolShowHour) hideElementsById(new Array("tiannetHourHead"),false);
if(m_bolShowMinute) hideElementsById(new Array("tiannetMinuteHead"),false);
hideElementsById(new Array("tiannetMonthHead","selTianYear","selTianHour","selTianMinute"),true);
}
//單擊小時span標簽響應
function spanHourCEvent(){
hideElementsById(new Array("tiannetYearHead","tiannetMonthHead"),false);
if(m_bolShowHour) hideElementsById(new Array("selTianHour"),false);
if(m_bolShowMinute) hideElementsById(new Array("tiannetMinuteHead"),false);
hideElementsById(new Array("tiannetHourHead","selTianYear","selTianMonth","selTianMinute"),true);
}
//單擊分鐘span標簽響應
function spanMinuteCEvent(){
hideElementsById(new Array("tiannetYearHead","tiannetMonthHead"),false);
if(m_bolShowHour) hideElementsById(new Array("tiannetHourHead"),false);
if(m_bolShowMinute) hideElementsById(new Array("selTianMinute"),false);
hideElementsById(new Array("tiannetMinuteHead","selTianYear","selTianMonth","selTianHour"),true);
}
//根據標簽id隱藏或顯示標簽
function hideElementsById(arrId,bolHide){
var strDisplay = "";
if(bolHide) strDisplay = "none";
for(var i = 0;i < arrId.length;i ++){
var obj = document.getElementById(arrId[i]);
obj.style.display = strDisplay;
}
}
//------------------ end 顯示日期時間的span標簽響應事件 ---------------------------//
//判斷某年是否為閏年
function isPinYear(year){
var bolRet = false;
if (0==year%4&&((year%100!=0)||(year%400==0))) {
bolRet = true;
}
return bolRet;
}
//得到一個月的天數,閏年為29天
function getMonthCount(year,month){
var c=m_aMonHead[month-1];
if((month==2)&&isPinYear(year)) c++;
return c;
}
//重新設置當前的日。主要是防止在翻年、翻月時,當前日大於當月的最大日
function setRealDayCount() {
if( tiannetDay > getMonthCount(tiannetYear,tiannetMonth) ) {
//如果當前的日大於當月的最大日,則取當月最大日
tiannetDay = getMonthCount(tiannetYear,tiannetMonth);
}
}
//在個位數前加零
function addZero(value){
if(value < 10 ){
value = "0" + value;
}
return value;
}
//取出空格
function tiannetTrim(str) {
return str.replace(/(^\s*)|(\s*$)/g,"");
}
//為select創建一個option
function createOption(objSelect,value,text){
var option = document.createElement("OPTION");
option.value = value;
option.text = text;
objSelect.options.add(option);
}
//往前翻 Year
function tiannetPrevYear() {
if(tiannetYear > 999 && tiannetYear <10000){tiannetYear--;}
else{alert("年份超出范圍(1000-9999)!");}
tiannetSetDay(tiannetYear,tiannetMonth);
//如果年份小於允許的最小年份,則創建對應的option
if( tiannetYear < tiannetYearSt ) {
tiannetYearSt = tiannetYear;
createOption(document.all.selTianYear,tiannetYear,tiannetYear + "年");
}
checkSelect(document.all.selTianYear,tiannetYear);
tiannetWriteHead();
}
//往後翻 Year
function tiannetNextYear() {
if(tiannetYear > 999 && tiannetYear <10000){tiannetYear++;}
else{alert("年份超出范圍(1000-9999)!");return;}
tiannetSetDay(tiannetYear,tiannetMonth);
//如果年份超過允許的最大年份,則創建對應的option
if( tiannetYear > tiannetYearEnd ) {
tiannetYearEnd = tiannetYear;
createOption(document.all.selTianYear,tiannetYear,tiannetYear + "年");
}
checkSelect(document.all.selTianYear,tiannetYear);
tiannetWriteHead();
}
//選擇今天
function tiannetToday() {
tiannetYear = tiannetDateNow.getFullYear();
tiannetMonth = tiannetDateNow.getMonth()+1;
tiannetDay = tiannetDateNow.getDate();
tiannetSetValue(true);
//tiannetSetDay(tiannetYear,tiannetMonth);
//selectObject();
}
//往前翻月份
function tiannetPrevMonth() {
if(tiannetMonth>1){tiannetMonth--}else{tiannetYear--;tiannetMonth=12;}
tiannetSetDay(tiannetYear,tiannetMonth);
checkSelect(document.all.selTianMonth,tiannetMonth);
tiannetWriteHead();
}
//往後翻月份
function tiannetNextMonth() {
if(tiannetMonth==12){tiannetYear++;tiannetMonth=1}else{tiannetMonth++}
tiannetSetDay(tiannetYear,tiannetMonth);
checkSelect(document.all.selTianMonth,tiannetMonth);
tiannetWriteHead();
}
//向span標簽中寫入年、月、時、分等數據
function tiannetWriteHead(){
document.all.tiannetYearHead.innerText = tiannetYear + "年";
document.all.tiannetMonthHead.innerText = tiannetMonth + "月";
if( m_bolShowHour ) document.all.tiannetHourHead.innerText = " "+tiannetHour + "時";
if( m_bolShowMinute ) document.all.tiannetMinuteHead.innerText = tiannetMinute + "分";
tiannetSetValue(false);//給文本框賦值,但不隱藏本控件
}
//設置顯示天
function tiannetSetDay(yy,mm) {
setRealDayCount();//設置當月真實的日
tiannetWriteHead();
var strDateFont1 = "", strDateFont2 = "" //處理日期顯示的風格
for (var i = 0; i < 37; i++){tiannetArrDay[i]=""}; //將顯示框的內容全部清空
var day1 = 1;
var firstday = new Date(yy,mm-1,1).getDay(); //某月第一天的星期幾
for (var i = firstday; day1 < getMonthCount(yy,mm)+1; i++){
tiannetArrDay[i]=day1;day1++;
}
//如果用於顯示日的最後一行的第一個單元格的值為空,則隱藏整行。
//if(tiannetArrDay[35] == ""){
// document.all.trTiannetDay5.style.display = "none";
//} else {
// document.all.trTiannetDay5.style.display = "";
//}
for (var i = 0; i < 37; i++){
var da = eval("document.all.tdTiannetDay"+i) //書寫新的一個月的日期星期排列
if (tiannetArrDay[i]!="") {
//判斷是否為周末,如果是周末,則改為紅色字體
if(i % 7 == 0 || (i+1) % 7 == 0){
strDateFont1 = "<font color=#f0000>"
strDateFont2 = "</font>"
} else {
strDateFont1 = "";
strDateFont2 = ""
}
da.innerHTML = strDateFont1 + tiannetArrDay[i] + strDateFont2;
//如果是當前選擇的天,則改變顏色
if(tiannetArrDay[i] == tiannetDay ) {
da.style.backgroundColor = "#CCCCCC";
} else {
da.style.backgroundColor = "#EFEFEF";
}
da.style.cursor="hand"
} else {
da.innerHTML="";da.style.backgroundColor="";da.style.cursor="default"
}
}//end for
tiannetSetValue(false);//給文本框賦值,但不隱藏本控件
}//end function tiannetSetDay
//根據option的值選中option
function checkSelect(objSelect,selectValue) {
var count = parseInt(objSelect.length);
if( selectValue < 10 && selectValue.toString().length == 2) {
selectValue = selectValue.substring(1,2);
}
for(var i = 0;i < count;i ++){
if(objSelect.options[i].value == selectValue){
objSelect.selectedIndex = i;
break;
}
}//for
}
//選中年、月、時、分等下拉框
function selectObject(){
//如果年份小於允許的最小年份,則創建對應的option
if( tiannetYear < tiannetYearSt ) {
for( var i = tiannetYear;i < tiannetYearSt;i ++ ){
createOption(document.all.selTianYear,i,i + "年");
}
tiannetYearSt = tiannetYear;
}
//如果年份超過允許的最大年份,則創建對應的option
if( tiannetYear > tiannetYearEnd ) {
for( var i = tiannetYearEnd+1;i <= tiannetYear;i ++ ){
createOption(document.all.selTianYear,i,i + "年");
}
tiannetYearEnd = tiannetYear;
}
checkSelect(document.all.selTianYear,tiannetYear);
checkSelect(document.all.selTianMonth,tiannetMonth);
if( m_bolShowHour ) checkSelect(document.all.selTianHour,tiannetHour);
if( m_bolShowMinute ) checkSelect(document.all.selTianMinute,tiannetMinute);
}
//給接受日期時間的控件賦值
//參數bolHideControl - 是否隱藏控件
function tiannetSetValue(bolHideControl){
var value = "";
if( !tiannetDay || tiannetDay == "" ){
tiannetOutObject.value = value;
return;
}
var mm = tiannetMonth;
var day = tiannetDay;
if( mm < 10 && mm.toString().length == 1) mm = "0" + mm;
if( day < 10 && day.toString().length == 1) day = "0" + day;
value = tiannetYear + tiannetDateSplit + mm + tiannetDateSplit + day;
if( m_bolShowHour ){
var hour = tiannetHour;
if( hour < 10 && hour.toString().length == 1 ) hour = "0" + hour;
value += tiannetDateTimeSplit + hour;
}
if( m_bolShowMinute ){
var minute = tiannetMinute;
if( minute < 10 && minute.toString().length == 1 ) minute = "0" + minute;
value += tiannetTimeSplit + minute;
}
tiannetOutObject.value = value;
//document.all.divTiannetDate.style.display = "none";
if( bolHideControl ) {
tiannetHideControl();
}
}
//是否顯示時間
function showTime(){
if( !m_bolShowHour && m_bolShowMinute){
alert("如果要選擇分鐘,則必須可以選擇小時!");
return;
}
hideElementsById(new Array("tiannetHourHead","selTianHour","tiannetMinuteHead","selTianMinute"),true);
if( m_bolShowHour ){
//顯示小時
hideElementsById(new Array("tiannetHourHead"),false);
}
if( m_bolShowMinute ){
//顯示分鐘
hideElementsById(new Array("tiannetMinuteHead"),false);
}
}
//彈出顯示日歷選擇控件,以讓用戶選擇
function tiannetPopCalendar(){
//隱藏下拉框,顯示相對應的head
hideElementsById(new Array("selTianYear","selTianMonth","selTianHour","selTianMinute"),true);
hideElementsById(new Array("tiannetYearHead","tiannetMonthHead","tiannetHourHead","tiannetMinuteHead"),false);
tiannetSetDay(tiannetYear,tiannetMonth);
tiannetWriteHead();
showTime();
var dads = document.all.divTiannetDate.style;
var iX, iY;
var h = document.all.divTiannetDate.offsetHeight;
var w = document.all.divTiannetDate.offsetWidth;
//計算left
if (window.event.x + h > document.body.offsetWidth - 10 )
iX = window.event.x - h - 5 ;
else
iX = window.event.x + 5;
if (iX <0)
iX=0;
//計算top
iY = window.event.y;
if (window.event.y + w > document.body.offsetHeight - 10 )
iY = document.body.scrollTop + document.body.offsetHeight - w - 5 ;
else
iY = document.body.scrollTop +window.event.y + 5;
if (iY <0)
iY=0;
dads.left = iX;
dads.top = iY;
tiannetShowControl();
selectObject();
}
//隱藏日歷控件(同時顯示被強制隱藏的標簽)
function tiannetHideControl(){
document.all.divTiannetDate.style.display = "none";
tiannetShowObject();
arrTiannetHide = new Array();//將被隱藏的標簽對象清空
}
//顯示日歷控件(同時隱藏會遮擋的標簽)
function tiannetShowControl(){
document.all.divTiannetDate.style.display = "";
tiannetHideObject("SELECT");
tiannetHideObject("OBJECT");
}
//根據標簽名稱隱藏標簽。如會遮住控件的select,object
function tiannetHideObject(strTagName) {
x = document.all.divTiannetDate.offsetLeft;
y = document.all.divTiannetDate.offsetTop;
h = document.all.divTiannetDate.offsetHeight;
w = document.all.divTiannetDate.offsetWidth;
for (var i = 0; i < document.all.tags(strTagName).length; i++)
{
var obj = document.all.tags(strTagName)[i];
if (! obj || ! obj.offsetParent)
continue;
// 獲取元素對於BODY標記的相對坐標
var objLeft = obj.offsetLeft;
var objTop = obj.offsetTop;
var objHeight = obj.offsetHeight;
var objWidth = obj.offsetWidth;
var objParent = obj.offsetParent;
while (objParent.tagName.toUpperCase() != "BODY"){
objLeft += objParent.offsetLeft;
objTop += objParent.offsetTop;
objParent = objParent.offsetParent;
}
//alert("控件左端:" + x + "select左端" + (objLeft + objWidth) + "控件底部:" + (y+h) + "select高:" + objTop);
var bolHide = true;
if( obj.style.display == "none" || obj.style.visibility == "hidden" || obj.getAttribute("Author") == "tiannet" ){
//如果標簽本身就是隱藏的,則不需要再隱藏。如果是控件中的下拉框,也不用隱藏。
bolHide = false;
}
if( ( (objLeft + objWidth) > x && (y + h + 20) > objTop && (objTop+objHeight) > y && objLeft < (x+w) ) && bolHide ){
//arrTiannetHide.push(obj);//記錄被隱藏的標簽對象
arrTiannetHide[arrTiannetHide.length] = obj;
obj.style.visibility = "hidden";
}
}
}
//顯示被隱藏的標簽
function tiannetShowObject(){
for(var i = 0;i < arrTiannetHide.length;i ++){
//alert(arrTiannetHide[i]);
arrTiannetHide[i].style.visibility = "";
}
}
//初始化日期。
function tiannetInitDate(strDate){
var arr = strDate.split(tiannetDateSplit);
tiannetYear = arr[0];
tiannetMonth = arr[1];
tiannetDay = arr[2];
}
//清空
function tiannetClear(){
tiannetOutObject.value = "";
tiannetHideControl();
}
//任意點擊時關閉該控件
function document.onclick(){
with(window.event.srcElement){
if (tagName != "INPUT" && getAttribute("Author") != "tiannet")
tiannetHideControl();
}
}
//按ESC鍵關閉該控件
function document.onkeypress(){
if( event.keyCode == 27 ){
tiannetHideControl();
}
}
Calendar.html:
復制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="ProgId" content="VisualStudio.HTML">
<meta name="Originator" content="Microsoft Visual Studio .NET 7.1">
<script src="./Calendar.js"></script>
</head>
<body>
(1)只選擇日期 <input type="text" name="date" readOnly onClick="setDay(this);"><br/>
(2)選擇日期和小時 <input type="text" name="dateh" readOnly onClick="setDayH(this);"><br/>
(3)選擇日期和小時及分鐘 <input type="text" name="datehm" readOnly onClick="setDayHM(this);">
</body>
</html>