经常在项目中有填写日期的工作,如果手动的输入当然可以,但是填写的日期往往和期望的不符.解决的方法有很多,这里主要阐述在Web项目中填写日历,自己动手开发一个电子日历,供用户选择合适的日期.

 基于Web的电子日历很简单,其原理如下:

    构造一个日历对象

    改变相应的年份和月份

    显示此年此月的月历.

  按照上述原理,我采用JS制作,原因是在各种环境中,都可以使用.现存在大多数Web开发环境都支持JS.当然,为了应用的简单,这里只针对IE6浏览器.

  

  第一步:如何在JS中构造对象?

/**
* 在javascript中创建类,使用function
* 属性this.f = 初值;
* 方法this.v = 方法名;
* 使用前先new 一个对象出来
* txtDate属性为最终显示日期的控件,从页面传近来,通过showOpen(txtDate)方法
*/
function ECal(txtDate)
{
 /*=============== 年月日 ===============*/
 d = new Date();
 this.year = d.getYear();
 this.month = d.getMonth()+1;
 this.day = d.getDate();
 this.txtDate = txtDate;
 this.win = null;
 this.x = 0;
 this.y = 0;

 /*===============得到此月总天数===============*/
 this.getDaysOfMonth = getDaysOfMonth;
 
 /*===============得到此月第一天是星期几===============*/
 this.getDayOfFirst = getDayOfFirst;
 
 /*=============== 显示月历 ======================*/
 this.showCal = showCal;
}

/*--- 得到指定年,指定月中的天数 ----*/
function getDaysOfMonth(year,month)
{
 switch(month)
 {
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12:
   return 31;
  case 2:
   if(year%4==0)
    return 29;
   else
    return 28;
  default:
   return 30;
 }
}

/*--------得到指定年,指定月 第一天是星期几,以便得出1号之前有多少空格要找印 */
function getDayOfFirst(year,month)
{
 /*
 Date对象,时间与日期对象
 getYear(): 得到年
 getMonth():得到月 0 ---- 11
 getDate(): 得到日 0 -----6
 getDay(): 得到星期
 */
 d = new Date(year,month-1,1);
 return d.getDay();
}

/**
*  在新窗口中显示日历
*  使用window.opener方法指示先前打开的窗口
*/
function showCal(year,month,day) 
{
}

posted on 2006-08-29 13:13  yudi  阅读(893)  评论(2编辑  收藏  举报