javascript日历控件

<script language="javascript">
<!--
var write_text_field;

var Selected_Month;
var Selected_Year;
var Current_Date = new Date();
var Current_Month = Current_Date.getMonth();//当前的月
var Current_Year = Current_Date.getYear();//当前的年
var Today = Current_Date.getDate();//当前的日期
var Days_in_Month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var Month_Label = new Array('一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月');
function Header(Year, Month)
{

   if (Month == 1)
   {
      if ((Year % 400 == 0) || ((Year % 4 == 0) && (Year % 100 != 0)))
  {
         Days_in_Month[1] = 29;
      }
   }
   var Header_String = Year + '年' + Month_Label[Month];
   return Header_String;
}
function Make_Calendar(Year, Month)
{
   var sYear=Year;
   var sMonth=Month;
   var HTML_String = '<table width=100% BORDER=0 CELLSPACING=0 cellpadding=0><tr><td valign="top">';
   HTML_String +=Cal(sYear, sMonth)+"";
   for(i=0;i<0;i++)
   {
    if (sMonth == 11)
{
         sMonth = 0;
         sYear++;
        }
else
{
         sMonth++;
        }
   HTML_String +=Cal(sYear, sMonth)+"";
   }
   HTML_String += '</td></tr></table>';
   document.all.Calendar.innerHTML = HTML_String;
}
function Cal(Year, Month)
{
   var First_Date = new Date(Year, Month, 1);
   var Heading = Header(Year, Month);
   var First_Day = First_Date.getDay() + 1;
   if (((Days_in_Month[Month] == 31) && (First_Day >= 6)) || ((Days_in_Month[Month] == 30) && (First_Day == 7)))
   {
      var Rows = 6;
   }
   else if ((Days_in_Month[Month] == 28) && (First_Day == 1))
   {
      var Rows = 4;
   }
   else
   {
      var Rows = 5;
   }
   var HTML_String = '';
   HTML_String += '<table width=100% BORDER=2 CELLSPACING=1 cellpadding=2 FRAME="box" background="image/bg' + (Month+1) + '.jpg" BORDERCOLORLIGHT="#FFFFFF" BORDERCOLORDARK="#FFFFFF">';//这里的图片也可以指定为一个单独的图片文件
   HTML_String += '<tr><th colspan=8  background="image/bg_r2_c2.jpg"><font color="#FFFFFF">' + Heading + '</font></th></tr>';
   HTML_String += '<tr><th ALIGN="CENTER"  BORDERCOLOR="FFFFFF"><font color="#FFFFFF">日</th><th ALIGN="CENTER"  BORDERCOLOR="FFFFFF"><font color="#FFFFFF">一</th><th ALIGN="CENTER"  BORDERCOLOR="FFFFFF"><font color="#FFFFFF">二</th><th ALIGN="CENTER"  BORDERCOLOR="FFFFFF"><font color="#FFFFFF">三</th>';
   HTML_String += '<th ALIGN="CENTER"  BORDERCOLOR="FFFFFF"><font color="#FFFFFF">四</th><th ALIGN="CENTER"  BORDERCOLOR="FFFFFF"><font color="#FFFFFF">五</th><th ALIGN="CENTER"  BORDERCOLOR="FFFFFF"><font color="#FFFFFF">六</th></tr>';
   var Day_Counter = 1;
   var Loop_Counter = 1;
   var l;
   var d;
   for (var j = 1; j <= Rows; j++)
   {
      HTML_String += '<tr ALIGN="left" VALIGN="top">';
      HTML_date='';
      for (var i = 1; i < 8; i++)
  {
         if ((Loop_Counter >= First_Day) && (Day_Counter <= Days_in_Month[Month]))
{
            if ((Day_Counter == Today) && (Year == Current_Year) && (Month == Current_Month))
{
selectDate = Year.toString() + '-' + (Month+1).toString() + '-' + Day_Counter.toString();
               HTML_String += '<td  BORDERCOLOR="FFFFFF"><strong><a href=javascript:writeDate("' + selectDate + '");' + '><font color="red">' + Day_Counter + '</font></a></strong></td>';
           
}
            else
{
selectDate = Year.toString() + '-' + (Month+1).toString() + '-' + Day_Counter.toString();
               HTML_String += '<td  BORDERCOLOR="FFFFFF"><strong><a href=javascript:writeDate("' + selectDate + '");' + '><font color="FFFFFF">' + Day_Counter + '</font></a></strong></td>';           
}
            HTML_date += '' + Year + '-' + (Month+1) + '-' + Day_Counter + ',';
            Day_Counter++;   
         }
         else
{
            HTML_String += '<td BORDERCOLOR="C0C0C0">&nbsp;</td>';
         }
         Loop_Counter++;
      }
   }
   HTML_String += '</table>';
   return HTML_String;
}
function getWeekDay(R,First_Day)
{
var Day_Counter = 1;
   var Loop_Counter = 1;
for (var j = 1; j <= Rows; j++)
{
      HTML_Date += '';
      for (var i = 1; i < 8; i++)
  {
         if ((Loop_Counter >= First_Day) && (Day_Counter <= Days_in_Month[Month]))
{
        
               HTML_Date += '' + Year + '-' + (Month+1) + '-' + Day_Counter + ',';
               Day_Counter++;   
         }
         Loop_Counter++;
      }
      HTML_Date += '';
}
}
function On_Year()
{
   var Year = document.when.year.value;
   if (Year.length == 4)
   {
      Selected_Month = document.when.month.selectedIndex;
      Selected_Year = Year;
      Make_Calendar(Selected_Year, Selected_Month);
   }
}
function On_Month()
{
   var Year = document.when.year.value;
   if (Year.length == 4)
   {
      Selected_Month = document.when.month.selectedIndex;
      Selected_Year = Year;
      Make_Calendar(Selected_Year, Selected_Month);
   }
   else
   {
      alert('请输入有效的年份!');
      document.when.year.focus();
   }
}
function Defaults()
{
   var Mid_Screen = Math.round(document.body.clientWidth / 2);
var Current_Date = new Date();
var Current_Month = Current_Date.getMonth();//当前的月
var Current_Year = Current_Date.getYear();//当前的年
   document.when.month.selectedIndex = Current_Month;
   document.when.year.value = Current_Year;
   Selected_Month = Current_Month;
   Selected_Year = Current_Year;
   Make_Calendar(Current_Year, Current_Month);
}
function Skip(Direction)
{
   if (Direction == '+')
   {
      if (Selected_Month == 11)
  {
         Selected_Month = 0;
         Selected_Year++;
      }
      else
  {
         Selected_Month++;
      }
   }
   else
   {
      if (Selected_Month == 0)
  {
         Selected_Month = 11;
         Selected_Year--;
      }
      else
  {
         Selected_Month--;
      }
   }
   Make_Calendar(Selected_Year, Selected_Month);
   document.when.month.selectedIndex = Selected_Month;
   document.when.year.value = Selected_Year;
}
function tick()
{
var hours, minutes, seconds, xfile;
var intHours, intMinutes, intSeconds;
var today;
today = new Date();
intHours = today.getHours();
intMinutes = today.getMinutes();
intSeconds = today.getSeconds();
if (intHours == 0)
{
hours = "12:";
xfile = "午夜 ";
}
else if (intHours < 12)
{
hours = intHours+":";
xfile = "上午 ";
}
else if (intHours == 12)
{
hours = "12:";
xfile = "正午 ";
}
else
{
intHours = intHours - 12
hours = intHours + ":";
xfile = "下午 ";
}
if (intMinutes < 10)
{
minutes = "0"+intMinutes+":";
}
else
{
minutes = intMinutes+":";
}
if (intSeconds < 10)
{
seconds = "0"+intSeconds+" ";
}
else
{
seconds = intSeconds+" ";
}
}
ie4=(document.all)?true:false;
ns4=(document.layers)?true:false;
//-->
</script>

<!-- 上面这些代码是通用的,可以放到include文件中 -->

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>javascript日历演示</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.style1 {
color: #FFFFFF;
font-weight: bold;
font-size: 14px;
}
-->
</style>
</head>

<body>

<!-- 这些代码是放置日历的位置的 -->
<div id=div32
style="HEIGHT: 80px; LEFT: 10px; POSITION: absolute; TOP: 25px; WIDTH: 180px; Z-INDEX: 1; visibility: hidden;">
<table width="150" border="1" BORDERCOLORLIGHT="#000000" BORDERCOLORDARK="#ffffff" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">
  <form name="when">
  <tr>
    <td align="center">
    <div id=NavBar style="position:relative;top:-1px;">
    <select name="month" onChange="On_Month()" class="select">
<script language="JavaScript1.2">
<!--
if (document.all){
   for (j=0;j<Month_Label.length;j++) {
      document.writeln('<option value=' + j + '>' + Month_Label[j]);
   }
}
//-->
</script>
    </select>
    <input type="text" name="year" size=7 maxlength=4 onKeyPress="return Check_Nums()" onKeyUp="On_Year()" class="input" value="<% =year(now()) %>"><br>
    </div>
    <input type="button" value="上月" onClick="Skip('-')" class="button">
    <input type="button" value="下月" onClick="Skip('+')" class="button">
</td>
</tr>
<tr><td id="Calendar" align="center"></td></tr>
</form>
</table>
</div>

<div id=alt style="display:none; position: absolute; left: 242; top: 67; width: 100; height: 42; Z-INDEX: 3; visibility: hidden;">
  <div id=aa style=" width: 100; height: 42;  border: 1 outset #000000;font-family: Tahoma; font-size: 9pt;">
  <div id=a1 style="color: #000000; font-weight: bold; background-color: #575757; text-align: center; border: 1 outset #FFFFFF; padding: 1;height:21"></div>
  <div id=a2 style="color: #000000; text-align:left; background-color: infobackground; border: 0 solid black;padding: 1;height:21"></div>
  </div>
</div>

<form action="new_contactOK.asp" method="post" name="frmNewCon" id="frmNewCon">
<input name="txtConDate" type="text" id="txtConDate" size="20">
<a href="javascript:toExit('show','div32','txtConDate')">
<img src="image/list_jrrc.gif" width="16" height="16" border="0" align="middle" onClick="Defaults();">
</a>
<br>
<SELECT NAME="sss">
<!-- 注意日历的位置最好不要和select控件重叠 -->
</SELECT><br>
<input name="txtConDate1" type="text" id="txtConDate1" size="20"><br>
<input name="txtConDate2" type="text" id="txtConDate2" size="20"><br>
<input name="txtConDate3" type="text" id="txtConDate3" size="20"><br>
<input name="txtConDate4" type="text" id="txtConDate4" size="20"><br>
</form>
</body>
</html>

<!-- 以下代码由于使用到了页面中的元素,所以只能在当前页面中填写,不能放到include文件中 -->
<script language="javascript">
<!--
function writeDate(ooo)
{
//这里是否可以想办法写成通用的呢?以便放入include文件中
document.frmNewCon.txtConDate.value=ooo;
toExit('hide','div32');
}
function toExit()
{
var args=toExit.arguments;
var visible=args[0];
write_text_field=args[2];
document.all.div32.style.left=0;
if(ns4)
{
theObj=eval("document.layers[\'"+args[1]+"\']");
if(theObj)theObj.visibility=visible;
}
else if(ie4)
{
if(visible=='show')visible='visible';
if(visible=='hide')visible='hidden';
theObj=eval("document.all[\'"+args[1]+"\']");
if(theObj)theObj.style.visibility=visible;
}
}
//-->
</script>

你可能感兴趣的:(JavaScript,html,J#,firefox)