简易日历

本文介绍了一个基于JavaScript实现的日历插件,该插件能够显示当前月份的日期,并允许用户通过按钮切换到上个月或下个月。插件还包含了自定义样式以突出当前日期。

代码演示

 

程序源码

     var  $  =   function (id) {  return  document.getElementById(id); };

    
var  Calendar  =   function (cID) {
        
this .calendar  =  $(cID);
        
var  D  =   new  Date();
        
this .year  =  D.getFullYear();
        
this .month  =  D.getMonth()  +   1 ;
        
this .date  =  D.getDate();
        
this .$getD  =   new  Date( this .year,  this .month,  0 ).getDate();
        
this .onEnd  =   function (){};
        
this .init();
    };
    Calendar.prototype 
=  {
        constructor: Calendar,
        
//  程序初始化
        init:  function () {
            
var  _this  =   this ;
            
this .drawC(  new  Date( this .year,  this .month,  0 ).getDate(),  new  Date( this .year,  this .month  -   1 1 ).getDay(),  new  Date().getDate());
            $(
' prevM ' ).onclick  =   function () { _this.prevM(); };
            $(
' nextM ' ).onclick  =   function () { _this.nextM(); };
            $(
' prevY ' ).onclick  =   function () { _this.prevY(); };
            $(
' nextY ' ).onclick  =   function () { _this.nextY(); };
        },
        
//  获得当前年月的日期数
        getDate:  function () {
            
return   new  Date( this .year,  this .month,  0 ).getDate();
        },
        
//  获取当月的第一天是星期几
        getDay:  function () {
            
return   new  Date( this .year,  this .month  -   1 1 ).getDay();
        },
        
//  画日历
        drawC:  function (max, start, now){
            
var  html  =   '' , j  =   0 , d  =   new  Date(), y  =  d.getFullYear(), m  =  d.getMonth()  +   1 ;
            html 
+=   ' <table id="LBody" width="212"><tbody><tr> ' ;
            
while (j ++   <  start) {
                html 
+=   ' <td></td> ' ;
            }
            
for ( var  i  =   0  ; i  <  max; i ++ ) {
                
if ((i  +   6   +  j)  %   7   ==   0 )  html  +=   ' <tr> ' ;
                html 
+=   ' <td><a class= '   +  ((y  ==   this .year  &&  m  ==   this .month  &&  (i  +   1 )   ==  now)   ?   ' now '  :  ' normal ' +   '  href="javascript:void(0);"> '   +  (i  +   1 +   ' </a></td> ' ;
                
if ( ! ((i  +  j  +   1 *   7 /   7 ) html  +=   ' </tr> ' ;
            }
            html 
+=   ' </tbody><table> ' ;
            
this .calendar.innerHTML  =  html;
            $(
' showDate ' ).innerHTML  =   this .year  +   ' . '   +   this .month;
            
this .onEnd();
        },
        
//  前一月
        prevM:  function () {
            
this .month -- ;
            
if ( this .month  <   1 ) {  this .month  =   12 ; this .year  -=   1 ; }
            
this .drawC( this .getDate(),  this .getDay(),  new  Date().getDate());
        },
        
//  后一月
        nextM:  function () {
            
this .month ++ ;
            
if ( this .month  >   12 ) {  this .month  =   1 ; this .year  +=   1 ; }
            
this .drawC( this .getDate(),  this .getDay(),  new  Date().getDate());
        },
        
//  前一年
        prevY:  function () {
            
this .year -- ;
            
this .drawC( this .getDate(),  this .getDay(),  new  Date().getDate());
        },
        
//  后一年
        nextY:  function () {
            
this .year ++ ;
            
this .drawC( this .getDate(),  this .getDay(),  new  Date().getDate());
        }
    }

 

使用说明

直接实例化即可

var  c  =   new  Calendar( ' CContainer ' );

 

代码示例下载

转载于:https://www.cnblogs.com/goodness2010/archive/2010/01/11/1644324.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值