想在自己的项目里添加一个万年历的功能。。哪位有代码。共享一下。谢谢!!

解决方案 »

  1.   

    var calendar=function(e,y,m){
    this.year = y ? y : new Date().getFullYear();
    this.month = m ? (m-1) : new Date().getMonth();
    this.today  = new Date(this.year,this.month);
    this.container = e;
    this.id = e;
    this.weekName = ['日','一','二','三','四','五','六'];
    this.event=null;
    this.daysArray=[];
    this.showWeekend=true; //是否将周末标注为红色
    this.showToday=true;
    }
    calendar.prototype = {
    //获取指定日期的星期
    getDay : function(d, m, y){
    y = y ? y : this.year;
    m = m || m == 0 ? m : this.month;
    return new Date(y,m,d).getDay();
    },
    //返回一月的天数
    monthDays : function(){
    if(1 == this.month)
    return (((this.year%4 == 0) && (this.year%100 != 0) || (this.year%400 == 0))? 29: 28);
    else
    return ([31,28,31,30,31,30,31,31,30,31,30,31][this.month]);
    },
    //创建日期矩阵
    createDaysArray : function(){
    var i,j = 0,md;
    md = this.monthDays(); 
    for(i = 1; i <= md; i++ ){
    if(0 == (i + this.getDay(1)-1)%7 || 0 == j){
    this.daysArray[j++] = [0,0,0,0,0,0,0];
    }
    this.daysArray[j-1][this.getDay(i)] = i;
    }
    },
    createTable : function(){
    var tmp = '' ,str = '' ,i,j,d,w;
    for ( i = 0 ; i < 7 ; i++ ){
    if( this.showWeekend &&  (i==0 || i==6) ){
    tmp += '<th id='+ this.id + '_w' + i +'><font color=red>' + this.weekName[i] + '</font></th>';
    }else{
    tmp += '<th id='+ this.id + '_w' + i +'>' + this.weekName[i] + '</th>';
    }
    }
    str = '<tr id=' + this.id + '_w' + '>' + tmp + '</tr>';
    for ( i =0 ; i < this.daysArray.length ; i ++ ){
    tmp = '';
    for(j=0;j<7;j++){
    if(this.daysArray[i][j]==0){
    tmp += ('<td> </td>');
    }else{
    w=new Date(this.year,this.month,this.daysArray[i][j]).getDay();
    if( this.showWeekend &&  (w==0 || w==6) ){
    tmp += ('<td id=' + this.id + '_d_' + this.year + '_' + ( this.month +1 ) + '_' + this.daysArray[i][j] + '><font color=red>' + this.daysArray[i][j]  + '</font></td>');
    }else{
    tmp += ('<td id=' + this.id + '_d_' + this.year + '_' + ( this.month +1 ) + '_' + this.daysArray[i][j] + '>' + this.daysArray[i][j]  + '</td>');
    }
    }
    }
    str += '<tr id=' + this.id + '_tr' + ( i + 1 ) + '>'+tmp+'</tr>';
    }
    str = '<table id=' + ( this.id + '_t' ) + '><tbody id=' + ( this.id + '_tb' ) + '>' + str + '</tbody></table>';
    $('#'+this.id).html(str);
    },
    isShowTips : function(st,et){
    st=new Date(st.getFullYear(),st.getMonth(),st.getDate());
    et=new Date(et.getFullYear(),et.getMonth(),et.getDate());
    return st.getTime()<=et.getTime();
    },
    addTips : function(e){
    this.event = e ? e : this.event;
    for (var i=0;i<this.event.length ;i++ ){
    this.handleAddTips(i);
    }
    },
    handleAddTips : function(eid){
    st = new Date(this.event[eid].st);
    et = new Date(this.event[eid].et);
    while(this.isShowTips(st,et)){
    e=$('#'+ this.id + '_d_' + st.getFullYear() + '_' + (st.getMonth() + 1) + '_' + st.getDate());
    $(e)
    .css('font-weight','bold')
    .css('cursor','hand')
    .attr('eid', $(e).attr('eid') ? ($(e).attr('eid') + '|' + eid ) : eid )
    .attr('et',(this.year + '/' + (this.month +1) ) + '/' + st.getDate() )
    .bind('click',function(){
    window.open('day.html?' + $(this).attr('et') ,null,'');
    })
    .hover(tips.show,tips.hide);
    st.setTime(st.getTime()+86400000);
    }
    },
    show:function(){
    this.createDaysArray();
    this.createTable();
    //标注今日
    var t=new Date();
    if(this.showToday && t.getMonth()==this.month){
    $("[id$='_d_"+t.getFullYear()+'_'+(t.getMonth()+1)+'_'+t.getDate()+"']")
    .attr('title','今天')
    .css('background','url(images/bg_cand.gif) no-repeat');
    }
    /*$("[id*='_d_']").hover(function(){
    $(this).css('border','1px #CCCCCC solid');
    },
    function(){
    $(this).css('border','');
    });
    */
    }
    }
    var _tips;
    var tips={
    init : function(w){
    w=w?w:80;
    $('#tips').html("<div class='panel' style='width:" + w + "px'><i class='corner'><i class='l1'></i><i class='l2'></i><i class='l3'></i><i class='l4'></i></i>  <div class='cbody'><div id='tipsTitle'></div><div id='tipsText'></div></div><i class='corner'><i class='l4'></i><i class='l3'></i><i class='l2'></i><i class='l5'></i></i></div>");
    $(document).bind('click',function(){
    clearTimeout(_tips);
    $('#tips').hide(200);
    }
    );
    },
    show : function(){
    clearTimeout(_tips);
    var str='';
    var p=$(this).offset();
    var eid=$(this).attr('eid').split('|');
    for(var i=0 ; i<eid.length ; i++ ){
    str+='<div title=\'' + eventlist[eid[i]].title + '\'>' + eventlist[eid[i]].title + '</div>';
    }
    $('#tipsTitle').html($(this).attr('et'));
    $('#tipsText').html(str);
    $('#tips').css('left',p.left+15);
    $('#tips').css('top',p.top+15);
    $('#tips').hide();
    $('#tips').show(200);
    },
    hide : function(){
    _tips=setTimeout("$('#tips').hide(200)",3000);
    }
    }