一个JavaScript的日历

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 alcoholwang 的 blog

改自blog上流行的日历,增加了一些效果,修改了外观,优化了代码。效果见左边吧。

  代码如下:

<script language="javascript">
 var months = new array("一", "二", "三","四", "五", "六", "七", "八", "九","十", "十一", "十二");
 var daysinmonth = new array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
 var days = new array("日","一", "二", "三","四", "五", "六");
 var classtemp;
 var today=new gettoday();
 var year=today.year;
 var month=today.month;
 var newcal;

 function getdays(month, year) {
  if (1 == month) return ((0 == year % 4) && (0 != (year % 100))) ||(0 == year % 400) ? 29 : 28;
  else return daysinmonth[month];
 }

 function gettoday() {
  this.now = new date();
  this.year = this.now.getfullyear();
  this.month = this.now.getmonth();
  this.day = this.now.getdate();
 }

 function calendar() {
  newcal = new date(year,month,1);
  today = new gettoday();  
  var day = -1;
  var startday = newcal.getday();
  var endday=getdays(newcal.getmonth(), newcal.getfullyear());
  var daily = 0;
  if ((today.year == newcal.getfullyear()) &&(today.month == newcal.getmonth()))
  {
   day = today.day;
  }
  var caltable = document.all.caltable.tbodies.calendar;
  var intdaysinmonth =getdays(newcal.getmonth(), newcal.getfullyear());

  for (var intweek = 0; intweek < caltable.rows.length;intweek++)
   for (var intday = 0;intday < caltable.rows[intweek].cells.length;intday++)
   {
    var cell = caltable.rows[intweek].cells[intday];
    var montemp=(newcal.getmonth()+1)<10?("0"+(newcal.getmonth()+1)):(newcal.getmonth()+1);        
    if ((intday == startday) && (0 == daily)){ daily = 1;}
    var daytemp=daily<10?("0"+daily):(daily);
    var d="<"+newcal.getfullyear()+"-"+montemp+"-"+daytemp+">";
    if(day==daily) cell.classname="daynow";
    else if(intday==6) cell.classname = "daysat";
    else if (intday==0) cell.classname ="daysun";
    else cell.classname="day";
    if ((daily > 0) && (daily <= intdaysinmonth))
    {
     cell.innertext = daily;
     daily++;
    } else
    {
     cell.classname="calendartd";
     cell.innertext = "";
    }
  }
  document.all.year.value=year;
  document.all.month.value=month+1;
 }

 function submonth()
 {
  if ((month-1)<0)
  {
   month=11;
   year=year-1;
  } else
  {
   month=month-1;
  }
  calendar();
 }

 function addmonth()
 {
  if((month+1)>11)
  {
   month=0;
   year=year+1;
  } else
  {
   month=month+1;
  }
  calendar();
 }

 function setdate()
 {
  if (document.all.month.value<1||document.all.month.value>12)
  {
   alert("月的有效范围在1-12之间!");
   return;
  }
  year=math.ceil(document.all.year.value);
  month=math.ceil(document.all.month.value-1);
  calendar();
 }
</script>

<script>
function buttonover()
{
 var obj = window.event.srcelement;
 obj.runtimestyle.csstext = "background-color:#ffffff";
// obj.classname="hover";
}

function buttonout()
{
 var obj = window.event.srcelement;
 window.settimeout(function(){obj.runtimestyle.csstext = "";},300);
}
</script>

<style>
input {font-family: verdana;font-size: 9pt;text-decoration: none;background-color: #ffffff;height: 20px;border: 1px solid #666666;color:#000000;}

.calendar {font-family: verdana;text-decoration: none;width: 170;background-color: #c0d0e8;font-size: 9pt;border:0px dotted #1c6fa5;}
.calendartd {font-family: verdana;font-size: 7pt;color: #000000;background-color:#f6f6f6;height: 20px;width:11%;text-align: center;}

.title {font-family: verdana;font-size: 11pt;font-weight: normal;height: 24px;text-align: center;color: #333333;text-decoration: none;background-color: #a4b9d7;border-top-width: 1px;border-right-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-bottom-style:1px;border-top-color: #999999;border-right-color: #999999;border-bottom-color: #999999;border-left-color: #999999;}

.day {font-family: verdana;font-size: 7pt;color:#243f65;background-color: #e5e9f2;height: 20px;width:11%;text-align: center;}
.daysat {font-family: verdana;font-size: 7pt;color:#ff0000;text-decoration: none;background-color:#e5e9f2;text-align: center;height: 18px;width: 12%;}
.daysun {font-family: verdana;font-size: 7pt;color: #ff0000;text-decoration: none;background-color:#e5e9f2;text-align: center;height: 18px;width: 12%;}
.daynow {font-family: verdana;font-size: 7pt;font-weight: bold;color: #000000;background-color: #ffffff;height: 20px;text-align: center;}

.daytitle {font-family: verdana;font-size: 9pt;color: #000000;background-color: #c0d0e8;height: 20px;width:11%;text-align: center;}
.daysattitle {font-family: verdana;font-size: 9pt;color:#ff0000;text-decoration: none;background-color:#c0d0e8;text-align: center;height: 20px;width: 12%;}
.daysuntitle {font-family: verdana;font-size: 9pt;color: #ff0000;text-decoration: none;background-color: #c0d0e8;text-align: center;height: 20px;width: 12%;}

.daybutton {font-family: webdings;font-size: 9pt;font-weight: bold;color: #243f65;cursor:hand;text-decoration: none;}

</style>


<table border="0" cellpadding="0" cellspacing="1" class="calendar" id="caltable">
<thead>
     <tr align="center" valign="middle">
  <td colspan="7" class="title">
   <a href="javascript:submonth();" title="上一月" class="daybutton">3</a> <input name="year" type="text" size="4" maxlength="4" onkeydown="if (event.keycode==13){setdate()}" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')"  onpaste="this.value=this.value.replace(/[^0-9]/g,'')"> 年 <input name="month" type="text" size="1" maxlength="2" onkeydown="if (event.keycode==13){setdate()}" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')"  onpaste="this.value=this.value.replace(/[^0-9]/g,'')"> 月 <a href="javascript:addmonth();" title="下一月" class="daybutton">4</a>
  </td>
 </tr>
 <tr align="center" valign="middle">
  <script language="javascript"> 
   document.write("<td class=daysuntitle id=diary >" + days[0] + "</td>");
   for (var intloop = 1; intloop < days.length-1;intloop++)
    document.write("<td class=daytitle id=diary>" + days[intloop] + "</td>");
    document.write("<td class=daysattitle id=diary>" + days[intloop] + "</td>");
  </script>
 </tr>
</thead>
<tbody border=1 cellspacing="0" cellpadding="0" id="calendar" align=center onclick="getdiary()">
 <script language="javascript">
  for (var intweeks = 0; intweeks < 6; intweeks++)
  {
   document.write("<tr style='cursor:hand'>");
   for (var intdays = 0; intdays < days.length;intdays++) document.write("<td class=calendartd onmouseover='buttonover();' onmouseout='buttonout();'></td>");
   document.write("</tr>");
  }
 </script>
</tbody>
</table>
<script  language="javascript">
 calendar();
</script>

本文关键:一个JavaScript的日历
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top