日期控件第二版 v1.02

本来打算昨天上传的,可是临发布的时候发现了一个隐藏的错误。只好推迟到今天。

v1.02赠加了两个下拉框选择年和月,提高了用户选择日期的速度.

还和v1.01一样下载下面两个文件。

Calendar_v1.02.txt 把文件名改为 Calendar_v1.02.js

CalendarDemo_v1.02.txt 把文件名改为 CalendarDemo_v1.02.htm 

IE 浏览器打开,点文本框后面的按钮就可以看到效果l.

可惜还是不能在 FireFox 下很好的运行.

/*
 Auther:  Nicholas.Meng
 Date:   2005-07-17
 Version: 1.02
 ****************************************************************************
 v1.02 提升:
        年和月的选择在按钮的前提下,提供下拉框选择,提高选择日期的速度。
        修改了得到每个月天数的方法 getMonthDaysNum(),原来那个原理未知,防止出错使用自己的方法。
    ****************************************************************************
 目的:
        为每个需要日期控件的输入框加一个它自己的日期控件,即一个<Div></Div>.
        刚创建的时候只赋上id,class等值,并且把它的位置固定在输入框的下方。这是日期控件只是一个空白层。
        显示的时候只向其中填内容而不需要再调整日期控件的位置。
        有效的避免了当浏览器窗口大小改变之后,使用getBoundingClientRect()只能得到控件相对位置,导致的日期控件错位的问题.
        并且就算用户没有点击按钮显示出日期控件.也只是增加了一个隐藏的空白层而已.
    ****************************************************************************
    如何调用:
        首先,使用日期控件的文本框一定要赋上id 和 name 并且在网页中均不可重复。
            主要方法:showCalendar( 文本框 , 日期控件的id );
            例如:
            <input type="text" id="txtDateFrom" name="txtDateFrom">
            <input type='Button' name="btnSchDaFr" οnclick="showCalendar( txtDateFrom , 'dcDateFrom')">

        之后,在网页代码的最末端加上如下代码:
            <script>
              createNewDiv( document.getElementById( 'txtDateFrom') , 'dcDateFrom' );
            </script>
   ****************************************************************************
   Tips:
        1.当需要多个日期控件时,所有的日期控件的 id 在网页中均不能重复.
        2.所有的日期控件DIV 都属于 Calendar class.
            可以在网页的<Style></Style>中做出字体等设置,例如:
            <style>
              .Calendar
              {
               font-size:12px;
               font-family:"Arial","sans-serif";
              }
            </style>
       3.控件上用到的颜色都作为变量放在前面,可以根据自己需要修改.
*/

var iCalendarHeight = 150;
var iCalendarWidth = 250;
var iBonder = 5;
var iMinYear = 1970;
var iMaxYear = 2030;
var iDaysPerPage = 42;

var sYearMthBgColor = '#395592';
var sYearMthFontColor = '#FFFFFF'
var sTabDateBgColor = '#CCCCCC';
var sTabTrWeekBgColor = '#F5F5F5';
var sTabTrDateBgColor = '#FFFFFF';
var sTabTdMouseOverColor= '#E1E1E1';
var sTabTdMouseOutColor = '#FFFFFF';
var sSelectedBgColor = '#420042';
var sNotSelectedFontColor = '#000000';
var sSundayFontColor = '#FF0000';
var sSaturdayFontColor = '#FF0000';
var sTabTdPrevAndNextFontColor = '#BBBBBB';
var sTabTodayBgColor = '#F5F5F5';
var sTabTodayLableFontColor = '#FF0000';

var isHid = true;
var arrDisplayedCalendars = new Array();

function createNewDiv( inputBox , sCalendarID )
{
    document.writeln( '<Div id='+ sCalendarID +' name= '+ sCalendarID +' class="Calendar" Author=Nick_For_His_Wife scrolling="no" frameborder=0 style="border:0px solid #EEEEEE ;position: absolute; width: '+iCalendarWidth+'; height: '+iCalendarHeight+'; z-index: 0 ; filter :/'progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#AAAAAA,strength='+iBonder+')/' ; display: none"></Div>' );
    positionDiv( inputBox , sCalendarID ) ;
}

function positionDiv( inputBox , sCalendarID)
{
    var FrLeft,FrTop,WinWidth,WinHeight;
    Winw=document.body.offsetWidth;
    Winh=document.body.offsetHeight;
    FrLeft = inputBox.getBoundingClientRect().left-2;
    FrTop = inputBox.getBoundingClientRect().top + inputBox.clientHeight;
    calendar = document.getElementById( sCalendarID );
    calendar.style.left = FrLeft;
    calendar.style.top = FrTop;
}

function getToday()
{
 return new Date();
}

function getDateFormat( dDate )
{
 var iYear = dDate.getFullYear();
 var iMth = ( dDate.getMonth() + 1 );
 var iDay = dDate.getDate();
 if ( iMth < 10 )
 {
  iMth = '0' + iMth;
 }
 if ( iDay < 10 )
 {
  iDay = '0' + iDay;
 }
 return iYear + '-' + iMth + '-' + iDay;
}

function isLeapYear( iYear )
{
  if ((iYear%400==0) || ((iYear%4==0) && (iYear%100!=0)))
  return true;
  else
   return false;
}

function getFitDate( iYear , iMth , iDay )
{
 var iActualMth = iMth + 1;
 // Not February
    if ( iActualMth != 2 )
 {
  if (( iDay == 31 )&&(( iActualMth == 4 )||( iActualMth == 6 )||( iActualMth == 9 )||( iActualMth == 11 )))
  {
   dNewDay = new Date( iYear , iMth , ( iDay - 1 ) );
   return dNewDay;
  }else
  {
   dNewDay = new Date( iYear , iMth , iDay );
   return dNewDay;
  }
 }
 // Is February
 if ( iDay < 29 )
 {
  dNewDay = new Date( iYear , iMth , iDay );
  return dNewDay;
 }
    // Leap Year
 if ( isLeapYear( iYear ) )
 {
  dNewDate = new Date( iYear , iMth , 29);
  return dNewDate;
 }
 // Not Leap Year
 dNewDate = new Date( iYear , iMth , 28);
 return dNewDate;
}

function getNextYear( calendar , inputBox , iYear , iMth , iDay )
{
    if ( ( iYear + 1 ) > iMaxYear )
 {
  dCurrDate = getFitDate( iMinYear , iMth , iDay );
 }
    else
 {
        dCurrDate = getFitDate( ( iYear + 1 ) , iMth , iDay );
    }
 displayCalendarFrame( calendar , inputBox , dCurrDate );
}

function getPreviousYear( calendar , inputBox , iYear , iMth , iDay )
{
 if ( ( iYear - 1 ) < iMinYear )
 {
  dCurrDate = getFitDate( inputBox , iMaxYear , iMth , iDay );
 }
    else
 {
   dCurrDate = getFitDate( ( iYear - 1 ) , iMth , iDay );
    }
 displayCalendarFrame( calendar , inputBox , dCurrDate );
}

function getNextMonth( calendar , inputBox , iYear , iMth , iDay )
{
    if ( ( iMth + 1 ) > 11 )
 {
  dCurrDate = getNextYear( calendar , inputBox , iYear , 0 , iDay );
  return;
 }
    else
 {
        dCurrDate = getFitDate( iYear , (iMth + 1) , iDay);
    }
 displayCalendarFrame( calendar , inputBox , dCurrDate );
}

function getPreviousMonth( calendar , inputBox , iYear , iMth , iDay )
{
    if ( ( iMth - 1 ) < 0 )
 {
  dCurrDate = getPreviousYear( calendar , inputBox , iYear , 11 , iDay );
  return;
 }
    else
 {
        dCurrDate = getFitDate( iYear , (iMth - 1) , iDay );
    }
 displayCalendarFrame( calendar , inputBox , dCurrDate );
}

function getMonthDaysNum( iYear , iMth )
{
 if ( iMth == 1 )
 {
        if ( isLeapYear( iYear ) )
        {
            return 29;
        }
        return 28;
    }
    switch ( iMth )
 {
        case 0 : return 31;
        case 2 : return 31;
        case 3 : return 30;
        case 4 : return 31;
        case 5 : return 30;
        case 6 : return 31;
        case 7 : return 31;
        case 8 : return 30;
        case 9 : return 31;
        case 10 : return 30;
        case 11 : return 31;
    }
}

function getAllDays( iYear , iMth )
{
    arrAllDays = new Array();
 dFirstDay = new Date( iYear , iMth , 1 );
 var iWeekDay = dFirstDay.getDay();
 if ( iWeekDay != 0 )
 {
        switch( iWeekDay )
        {
      case 6 : arrAllDays.push( 0 );
      case 5 : arrAllDays.push( 0 );
      case 4 : arrAllDays.push( 0 );
      case 3 : arrAllDays.push( 0 );
      case 2 : arrAllDays.push( 0 );
      case 1 : arrAllDays.push( 0 );
     }
    }
 var iMthDaysNum = getMonthDaysNum( iYear , iMth );
 for (var i = 0 ; i < iMthDaysNum ; i++ )
 {
  arrAllDays.push( i + 1 );
 }
 var iArrLen = arrAllDays.length;
 for( var i=iArrLen ; i < iDaysPerPage ; i++ )
 {
     arrAllDays.push( 0 );
 }
 return arrAllDays;
}

function getValidDate( sDate )
{
 arrDate = sDate.split( "-" );
 if ( arrDate.length != 3 ) // if it is not valid format return today
 {
  return new Date();
 }
 var iYear = arrDate[0]
 var iMth = arrDate[1];
 var iDay = arrDate[2];
 return getFitDate( iYear , (iMth-1) , iDay);
}

function showCalendar( inputBox , sCalendarID )
{
 closeAllDisplayedCalendar();
    addCalenderDisplay( sCalendarID );
 var sValue = inputBox.value;
 calendar = document.getElementById( sCalendarID );
 dCurrDate = getValidDate( sValue );
 displayCalendarFrame( calendar , inputBox , dCurrDate );
 isHid = false;
}

function displayCalendarFrame( calendar , inputBox , dCurrDate )
{
    var FrameText;
 var sSelect;
    var FrLeft,FrTop,WinWidth,WinHeight;
    var iYear = dCurrDate.getFullYear();
    var iMth = dCurrDate.getMonth();
    var iDay = dCurrDate.getDate();
    arrAllDays = getAllDays( iYear , iMth );
    frameText="/n<table onselectstart=/"return false;/" border='0' cellpadding='0' cellspacing='0' bgcolor=/'"+ sYearMthBgColor+"/' width='100%' height='15' style=/"color:/'"+ sYearMthFontColor +"/';font-weight:bolder;border:0px solid/">"+"/n<tr>/n";
    frameText+="<td width=8>";
    frameText+="<input type='Button' value='&lt;&lt;' width='8' height='11' border='0' style='cursor:hand' οnclick=/"parent.getPreviousYear(window.parent.document.all."+ calendar.name +", window.parent.document.all." + inputBox.name + "," + iYear + "," + iMth + "," + iDay + ")/">";
    frameText+="</td>/n";
    frameText+="<td vAlign=middle align='center'>";
    frameText+="<select bgColor=/'"+ sYearMthBgColor +"/' fontColor=/'"+ sYearMthFontColor +"/' id='"+calendar.id+"SelectYear' name='"+calendar.id+"SelectYear' onClick=/"isHid=false;/" onChange=/"showSelectYear( window.parent.document.all."+inputBox.name + ",window.parent.document.all."+ calendar.name +", window.parent.document.all."+calendar.id+"SelectYear ," + iMth + " , "+ iDay +" )/">";
 frameText+=initSelectYearOption( iYear );
 frameText+="</select>";
    frameText+="</td>/n";
    frameText+="<td width=8>";
    frameText+="<input type='Button' value='&gt;&gt;' width='8' height='11' border='0' style='cursor:hand' οnclick=/"parent.getNextYear(window.parent.document.all."+ calendar.name +", window.parent.document.all."+ inputBox.name + "," + iYear + "," + iMth + "," + iDay + ")/">";
    frameText+="</td>/n";
    frameText+="<td width=8>";
    frameText+="<input type='Button' value='&lt;' width='8' height='11' border='0' style='cursor:hand' οnclick=/"parent.getPreviousMonth(window.parent.document.all."+ calendar.name +", window.parent.document.all."+inputBox.name+"," + iYear + "," + iMth + "," + iDay + ")/">";
    frameText+="</td>/n";
    frameText+="<td vAlign=middle align='center' width='50'>";
 frameText+="<select bgColor=/'"+ sYearMthBgColor +"/' fontColor=/'"+ sYearMthFontColor +"/' id='"+calendar.id+"SelectMonth' name='"+calendar.id+"SelectMonth' onClick=/"isHid=false;/" onChange=/"showSelectMth( window.parent.document.all."+inputBox.name + ",window.parent.document.all."+ calendar.name +"," + iYear + ", window.parent.document.all."+calendar.id+"SelectMonth , " + iDay +" )/">";
 frameText+=initSelectMthOption( iMth );
 frameText+="</select>";
    frameText+="</td>/n";
    frameText+="<td width=8>";
    frameText+="<input type='Button' value='&gt;' width='8' height='11' border='0' style='cursor:hand' οnclick=/"parent.getNextMonth (window.parent.document.all."+ calendar.name +", window.parent.document.all." + inputBox.name + "," + iYear + "," + iMth + "," + iDay + ")/">";
    frameText+="</td>"+"/n";
    frameText+="</tr>"+"/n";
    frameText+="</table>"+"/n";
    frameText+="<table onselectstart=/"return false;/" border='0' cellpadding='0' cellspacing='1' width='100%' bgcolor='"+sTabDateBgColor+"'>"+"/n";
    frameText+="<tr bgcolor="+ sTabTrWeekBgColor +">"+"/n";
    frameText+="<td><center><font color="+sSundayFontColor+">Sun</font></center></td>"+"/n";
    frameText+="<td><center>Mon</center></td>"+"/n";
    frameText+="<td><center>Tue</center></td>"+"/n";
    frameText+="<td><center>Wed</center></td>"+"/n";
    frameText+="<td><center>Thu</center></td>"+"/n";
    frameText+="<td><center>Fri</center></td>"+"/n";
    frameText+="<td><center><font color="+sSaturdayFontColor+">Sat</center></td>"+"/n";
    frameText+="</tr>"+"/n";
    var arrDaysLength = arrAllDays.length;
    var sValue = 0;
    for( var i=0 ; i < arrDaysLength ; i+=7 )
    {
        frameText+="<tr bgcolor="+sTabTrDateBgColor+">"+"/n";
        for( var iCol = 0 ; iCol < 7 ; iCol++ )
        {
            sValue = arrAllDays[ i + iCol ];
            if ( sValue == 0 ) // Previous or Next Month Day.
            {
                frameText+= "<td width=35><center><font color="+sTabTdPrevAndNextFontColor+">"+"."+"</font></center></td>"+"/n"
                continue;
            }
            sValue = sValue - 0;
            if ( sValue == iDay ) // selected Date
            {
                frameText+="<td width=35 style=/"background:"+sSelectedBgColor+";cursor:hand/" onClick=/"parent.outputValue(window.parent.document.all."+inputBox.name+","+ iYear +","+ iMth +","+ sValue +");parent.closeCalendar(window.parent.document.all." + calendar.name + ");/"><center><font color='#FFFFFF'>"+sValue+"</font></center></td>"+"/n";
                continue;
            }
            // current month not select days.
            if ( iCol == 0 ) // Sunday
            {
                frameText+="<td width=35 style=/"cursor:hand/" onMouseOver=/"this.style.background=/'"+ sTabTdMouseOverColor +"/'/" onMouseOut=/"this.style.background=/'"+sTabTdMouseOutColor+"/'/" onClick=/"parent.outputValue(window.parent.document.all."+inputBox.name+","+ iYear +","+ iMth +","+ sValue +");parent.closeCalendar(window.parent.document.all." + calendar.name + ");/"><center><font color="+sSundayFontColor+">"+sValue+"</font></center></td>"+"/n";
                continue;
            }
            if ( iCol == 6 ) // Saturday
            {
                frameText+="<td width=35 style=/"cursor:hand/" onMouseOver=/"this.style.background=/'"+ sTabTdMouseOverColor+"/'/" onMouseOut=/"this.style.background=/'"+sTabTdMouseOutColor+"/'/" onClick=/"parent.outputValue(window.parent.document.all."+inputBox.name+","+ iYear +","+ iMth +","+ sValue +");parent.closeCalendar(window.parent.document.all." + calendar.name + ");/"><center><font color="+ sSaturdayFontColor+">"+sValue+"</font></center></td>"+"/n";
                continue;
            }
            frameText+="<td width=35 style=/"cursor:hand/" onMouseOver=/"this.style.background=/'"+sTabTdMouseOverColor+"/'/" onMouseOut=/"this.style.background=/'"+sTabTdMouseOutColor+"/'/" onClick=/"parent.outputValue(window.parent.document.all."+inputBox.name+","+ iYear +","+ iMth +","+ sValue +");parent.closeCalendar(window.parent.document.all." + calendar.name + ");/"><center><font color=/'"+sNotSelectedFontColor+"/'>"+sValue+"</font></center></td>"+"/n";
        }
    }
    frameText+="</tr>"+"/n";
    frameText+="</table>"+"/n";
    frameText+="<table onselectstart=/"return false;/" cellpadding='0' cellspacing='0' bgcolor="+ sTabTodayBgColor +" width='100%' height='15'>"+"/n<tr>/n";
    dToday = getToday();
    var iTodayYear = dToday.getFullYear();
    var iTodayMth = dToday.getMonth();
    var iTodayDay = dToday.getDate();
    var sToday = getDateFormat( dToday );
    frameText+="<td width=/'" + iCalendarWidth + "/' title=/"Today: "+ sToday+"/" style=/"cursor:hand/" οnclick=/"parent.outputValue(window.parent.document.all."+inputBox.name+ "," + iTodayYear + "," + iTodayMth + "," + iTodayDay +");parent.closeCalendar(window.parent.document.all." + calendar.name + ");/">";
    frameText+="<font color="+sTabTodayLableFontColor+">Today: </font>"+sToday;
    frameText+="</td>/n";
    frameText+="<td>";
    frameText+="<input type='Button' width='100%' border='0' value='x' style='cursor:hand' οnclick=/"parent.closeCalendar(window.parent.document.all." + calendar.name + ");/">";
    frameText+="</td>/n";
    frameText+="</tr>/n";
    calendar.innerHTML = frameText;
    calendar.style.display="";
}

function addCalenderDisplay( sCalendarID )
{
    var iArrLength = arrDisplayedCalendars.length;
    if ( iArrLength ==0 )
    {
        arrDisplayedCalendars.push( sCalendarID );
        return;
    }
    var sValue;
    for( var i=0 ; i < iArrLength ; i++ )
    {
        sValue = arrDisplayedCalendars[ i ];
        if ( sValue == sCalendarID )
        {
            return;
        }
    }
    arrDisplayedCalendars.push( sCalendarID );
}

function removeCalendarDisplay( sCalendarID )
{
    var iArrLength = arrDisplayedCalendars.length;
    if ( iArrLength == 0 )
    {
        return;
    }
    var sValue;
    for( var i=0 ; i < iArrLength ; i++ )
    {
        sValue = arrDisplayedCalendars[ i ];
        if ( sValue == sCalendarID )
        {
            arrDisplayedCalendars.splice( i , 1 );
            return;
        }
    }
}

function outputValue( inputBox , iYear , iMth , iDay )
{
    dCurrDate = new Date( iYear , iMth , iDay );
    inputBox.value = getDateFormat( dCurrDate );
}

function closeCalendar( calendar )
{
    calendar.style.display="none";
    removeCalendarDisplay( calendar.id );
}

function closeAllDisplayedCalendar()
{
    if (isHid)
    {
        var iArrLength = arrDisplayedCalendars.length;
        if ( iArrLength == 0 )
        {
            return;
        }
        var sValue;
        for ( var i=0 ; i < iArrLength ; i++ )
        {
            sValue = arrDisplayedCalendars[ i ];
            calendar = document.getElementById( sValue );
            if ( calendar != null )
            {
                calendar.style.display = "none";
                arrDisplayedCalendars.splice( i , 1 );
            }
        }
    }
    isHid = true;
}

function showSelectMth( inputBox , calendar , iYear , selectMth ,iDay )
{
    var iSelectMth = selectMth.options[ selectMth.selectedIndex ].value ;
 dSelect = getFitDate( iYear , ( iSelectMth - 1) , iDay );
 displayCalendarFrame( calendar , inputBox , dSelect );
 isHid = true;
}

function showSelectYear( inputBox , calendar , selectYear , iMth ,iDay )
{
    var iSelectYear = selectYear.options[ selectYear.selectedIndex ].value ;
 dSelect = getFitDate( iSelectYear , iMth , iDay );
 displayCalendarFrame( calendar , inputBox , dSelect );
 isHid = true;
}

function initSelectMthOption( iMth )
{
    var sCode;
    var iActualMth = iMth + 1;
    for ( var i=1 ; i<=12 ; i++ )
    {
        if ( i == iActualMth )
        {
            sCode += '<option value='+ i +' selected>'+i;
        }
        else
        {
            sCode += '<option value='+ i +'>'+i;
        }
    }
    return sCode;
}

function initSelectYearOption( iYear )
{
    var sCode ;
    for ( var i= iMinYear ; i <= iMaxYear ; i++ )
    {
        if ( i ==iYear )
        {
            sCode += '<option value='+ i +' selected>'+i;
        }
        else
        {
            sCode += '<option value='+ i +'>'+i;
        }
    }
    return sCode;
}
// click other side of current web page will close calendar.
document.onclick = closeAllDisplayedCalendar;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值