`

JS创建日历控件

阅读更多
// JavaScript Document
/********************************************************************
主题:基于CSS&LI的日历显示
作者:Smart/Issac
说明:
1.调用GetCalendar(InputBoxId);
2.问题:IE5下隐含框架zIndex属性不支持;Mozilla下尚不支持若干方法.
********************************************************************/

/*创建框架*/
document.writeln("<div id=\"calendar\"></div>");
document.writeln("<iframe id=\"calendarBox\" scrolling=\"no\" frameborder=\"0\" style=\"display:none; position:absolute;\"></iframe>");
var cDiv = document.getElementById("calendar");
var cFrame = document.getElementById("calendarBox");
/*基本参数*/
var meWidth = cDiv.clientWidth; //日历宽度
var meHeight= cDiv.clientHeight; //日历高度
var isHidden=true; //日历是否关闭

/*取得今日日期*/
function GetTodayDate() {
today=new Date();
yy=today.getYear();
mm=(today.getMonth() + 1);
if (mm<10)
   mm='0'+mm;
dd=today.getDate();
if (dd<10)
   dd='0'+dd;
return yy+'-'+mm+'-'+dd
}
/*输入今天日期*/
function SetTodayDate(InputBoxId) {
HiddenCalendar();
document.getElementById(InputBoxId).value=GetTodayDate();
}
/*取某年某月第一天的星期值(月份-1)*/
function GetFirstWeek(theYear,theMonth) {
return (new Date(theYear,theMonth-1,1)).getDay();
}
/*取某年某月中总天数*/
function GetThisDays(theYear,theMonth) {
return (new Date(theYear,theMonth,0)).getDate();
}
/*取某年某月上个月中总天数*/
function GetLastDays(theYear,theMonth) {
return (new Date(theYear,theMonth-1,0)).getDate();
}
/*判断是否是闰年*/
function LeapYear(theYear) {
if ((theYear%400==0) || ((theYear%4==0) && (theYear%100!=0)))
   return true;
else
   return false;
}
/*判断日期(YYYY-MM-DD)的日期是否正确*/
function DateIsTrue(asDate) {
var lsDate=asDate + "";
var loDate=lsDate.split("-");
if (loDate.length!=3)
   return false;
var liYear=parseFloat(loDate[0]);
var liMonth=parseFloat(loDate[1]);
var liDay=parseFloat(loDate[2]);
if ((loDate[0].length>4)||(loDate[1].length>2)||(loDate[2].length>2))
   return false;
if (isNaN(liYear)||isNaN(liMonth)||isNaN(liDay))
   return false;
if ((liYear<1800)||(liYear>2500))
   return false;
if ((liMonth>12)||(liMonth<=0))
   return false;
if (GetThisDays(liYear,liMonth)<liDay)
   return false;
return !isNaN(Date.UTC(liYear,liMonth,liDay));
}
/*输入框显示*/
function InputValue(InputBoxId,Year,Month,Day) {
if (Month<10)
   Month='0'+Month;
if (Day<10)
   Day='0'+Day;
document.getElementById(InputBoxId).value=Year+"-"+Month+"-"+Day;
}
/*上一月*/
function PrevMonth(InputBoxId,Year,Month,Day) {
Month=Month-1;
if (Month<1) {
   Month=12;
   Year=Year-1;
   if (Year<1800)
    Year=2500;
}
Day=((GetThisDays(Year,Month)<Day)?GetThisDays(Year,Month):Day);
isHidden=false;
ShowCalendar(InputBoxId,Year,Month,Day);
}
/*下一月*/
function NextMonth(InputBoxId,Year,Month,Day) {
Month=Month+1;
if (Month>12) {
   Month=1;
   Year=Year+1;
   if (Year>2500)
    Year=1800;
}
Day=((GetThisDays(Year,Month)<Day)?GetThisDays(Year,Month):Day);
isHidden=false;
ShowCalendar(InputBoxId,Year,Month,Day);
}
/*上一年*/
function PrevYear(InputBoxId,Year,Month,Day) {
Year=Year-1;
if (Year<1800)
   Year=2500;
Day=((GetThisDays(Year,Month)<Day)?GetThisDays(Year,Month):Day);
isHidden=false;
ShowCalendar(InputBoxId,Year,Month,Day);
}
/*下一年*/
function NextYear(InputBoxId,Year,Month,Day) {
Year=Year+1;
if (Year>2500)
   Year=1800;
Day=((GetThisDays(Year,Month)<Day)?GetThisDays(Year,Month):Day);
isHidden=false;
ShowCalendar(InputBoxId,Year,Month,Day);
}
/*根据输入框中的日期显示日历(调用方法)*/
function GetCalendar(InputBoxId) {
isHidden=false;
var boxValue=document.getElementById(InputBoxId).value;
if (DateIsTrue(boxValue)) {
   loDate=boxValue.split("-");
   YY=parseFloat(loDate[0]);
   MM=parseFloat(loDate[1]);
   DD=parseFloat(loDate[2]);
   ShowCalendar(InputBoxId,YY,MM,DD);
}
else {
   today=new Date();
   yy=today.getYear();
   mm=(today.getMonth() + 1);
   dd=today.getDate();
   ShowCalendar(InputBoxId,yy,mm,dd);
}
}
/*隐藏日历*/
function HiddenCalendar() {
cDiv.style.display="none";
cFrame.style.display = "none";
}
function CloseCalendar() {
if (isHidden){
   cDiv.style.display="none";
   cFrame.style.display = "none";
}
isHidden=true;
}
/*显示日历*/
function ShowCalendar(InputBoxId,theYear,theMonth,theDay) {
var nowYear=(theYear==null?1981:theYear);
var nowMonth=(theMonth==null?10:theMonth);
var nowDay=(theDay==null?7:theDay);
var fw=GetFirstWeek(nowYear,nowMonth);
var nfw=GetFirstWeek(nowYear,nowMonth+1);
var ld=GetLastDays(nowYear,nowMonth);
var td=GetThisDays(nowYear,nowMonth);
var w=0;
var FrameContent;
var meLeft,meTop,winWidth,winHeight;
var monthText = new Array("01月","02月","03月","04月","05月","06月","07月","08月","09月","10月","11月","12月"); //月份文本
var weekText = new Array("sun","mon","tue","wed","thu","fri","sat"); //星期文本
//var weekText = new Array("日","一","二","三","四","五","六"); //星期文本
//显示的位置
winWidth=document.body.offsetWidth;
winHeight=document.body.offsetHeight;
if (document.getElementById(InputBoxId).getBoundingClientRect) { // For IE
   meLeft=document.getElementById(InputBoxId).getBoundingClientRect().left+3;
   meTop=document.getElementById(InputBoxId).getBoundingClientRect().top+document.getElementById(InputBoxId).clientHeight+3;
}
else if (document.getBoxObjectFor) { // For Mozilla
   meLeft=document.getBoxObjectFor(document.getElementById(InputBoxId)).x+3+"px";
   meTop=document.getBoxObjectFor(document.getElementById(InputBoxId)).y+document.getElementById(InputBoxId).clientHeight+3+"px";
}
if (((meLeft+meWidth)>winWidth)&&(meWidth<winWidth))
   meLeft=winWidth-meWidth;
if ((meTop+meHeight>winHeight)&&(meHeight<winHeight))
   meTop=winHeight-meHeight;
cDiv.style.display="";
cDiv.style.left=meLeft;
cDiv.style.top=meTop;
//显示日历内容
FrameContent="<div id=\"title\">";
FrameContent+="<p id=\"year\"><a class=\"btnPrev\" href=\"javascript:parent.PrevYear ('"+InputBoxId+"',"+nowYear+","+nowMonth+","+nowDay+")\" title=\"上一年\">&laquo;</a>"+nowYear+"年<a class=\"btnNext\" href=\"javascript:parent.NextYear ('"+InputBoxId+"',"+nowYear+","+nowMonth+","+nowDay+")\" title=\"下一年\">&raquo;</a>&nbsp;</p>";
FrameContent+="<p id=\"month\"><a class=\"btnPrev\" href=\"javascript:parent.PrevMonth ('"+InputBoxId+"',"+nowYear+","+nowMonth+","+nowDay+")\" title=\"上一个月\">&laquo;</a>"+monthText[nowMonth-1]+"<a class=\"btnNext\" href=\"javascript:parent.NextMonth ('"+InputBoxId+"',"+nowYear+","+nowMonth+","+nowDay+")\" title=\"下一个月\">&raquo;</a></p>";
FrameContent+="</div>";
FrameContent+="<div id=\"week\">";
FrameContent+="<ul>";
for (i=0; i<7; i++)
   FrameContent+="<li>"+weekText[i]+"</li>";
FrameContent+="</ul>";
FrameContent+="</div>";
FrameContent+="<div id=\"day\">";
FrameContent+="<ul>";
if (fw!=0){ //第一行上月日期
   for (i=(ld-fw+1);i<=ld;i++) {
    if ((w%7)==0)
     FrameContent+="<li class=\"otherSun\" onclick=\"parent.PrevMonth ('"+InputBoxId+"',"+nowYear+","+nowMonth+","+i+")\"><a href=\"javascript:;\">"+i+"</a></li>";
    else
     FrameContent+="<li class=\"otherDay\" onclick=\"parent.PrevMonth ('"+InputBoxId+"',"+nowYear+","+nowMonth+","+i+")\"><a href=\"javascript:;\">"+i+"</a></li>";
    w++;
   }
}
for (i=1; i<=td; i++) {
   if ((w%7)==0)
    FrameContent+="<li class=\"theSun\" onclick=\"parent.InputValue('"+InputBoxId+"',"+nowYear+","+nowMonth+","+i+");parent.HiddenCalendar()\"><a href=\"javascript:;\">"+i+"</a></li>";
   else if ((w%7)==6)
    FrameContent+="<li class=\"theSat\" onclick=\"parent.InputValue('"+InputBoxId+"',"+nowYear+","+nowMonth+","+i+");parent.HiddenCalendar()\"><a href=\"javascript:;\">"+i+"</a></li>";
   else
    FrameContent+="<li class=\"normal\" onclick=\"parent.InputValue('"+InputBoxId+"',"+nowYear+","+nowMonth+","+i+");parent.HiddenCalendar()\"><a href=\"javascript:;\">"+i+"</a></li>";
   w++;
}
if (nfw!=0)
   for (i=1; i<=(7-nfw); i++)
    FrameContent+="<li class=\"otherDay\" onclick=\"parent.NextMonth('"+InputBoxId+"',"+nowYear+","+nowMonth+","+i+")\"><a href=\"javascript:;\">"+i+"</a></li>";
FrameContent+="</ul>";
FrameContent+="</div>";
FrameContent+="<div id=\"option\">";
FrameContent+="<a id=\"calendarToday\" href=\"javascript:parent.SetTodayDate('"+InputBoxId+"')\" title=\""+GetTodayDate()+"\">今天</a>";
FrameContent+="<a id=\"calendarCancel\" href=\"javascript:parent.HiddenCalendar()\">取消</a>";
FrameContent+="</div>";
cDiv.innerHTML=FrameContent;
cDiv.style.display="block";
cDiv.style.zIndex = 1107;
//鼠标经过效果以及周末颜色标志
var weekLi = document.getElementById("week").getElementsByTagName("li");
weekLi[0].className = "sun";
weekLi[6].className = "sat";
var dayLi = document.getElementById("day").getElementsByTagName("li");
for (var i=0; i<dayLi.length; i++) {
   if (dayLi[i].className != "otherDay" && dayLi[i].className != "otherSun" && dayLi[i].innerText == nowDay)
    dayLi[i].id = "now";
   else {
    dayLi[i].onmouseover = function(){
     this.style.background = "#f5f5f5";
    }
    dayLi[i].onmouseout = function(){
     this.style.background = "#fff";
    }
   }
}
//定义背景框架样式
cFrame.style.width = cDiv.offsetWidth;
cFrame.style.height = cDiv.offsetHeight;
cFrame.style.top = cDiv.style.top;
cFrame.style.left = cDiv.style.left;
cFrame.style.zIndex = cDiv.style.zIndex - 1;
cFrame.style.display = "block";
}
document.onclick = CloseCalendar;

/***CSS*********************************************************************************************************************/

/* CSS Document */
/* Author: Issac */
/* Date: October,2005 */
*
{
margin:0px;
padding:0px;
}
#calendar {
position: absolute;
width: 182px;
voice-family: "\"}\"";
voice-family: inherit;
width: 183px;
}
#calendar div {
font: 11px Verdana, Arial, Helvetica, sans-serif;
text-align: center;
}
#calendar a {
text-decoration: none;
}
/*日历标题(年月)*/
#calendar #title {
height: 24px;
background: url(calendar_title.gif) no-repeat left;
color: #666;
font-weight: bold;
line-height: 24px;
text-align: right;
width: 182px;
voice-family: "\"}\"";
voice-family: inherit;
width: 183px;
}
.Issac{}
#calendar #title p {
display: inline;
}
#calendar #title a {
width: 14px;
margin: 0px 2px;
background-repeat: no-repeat;
background-position: left;
color: #f60;
text-indent: -9999px;
}
#calendar #title a.btnPrev {
background-image: url(btn_prev.gif);
}
#calendar #title a.btnNext {
background-image: url(btn_next.gif);
}
/*日历星期*/
#calendar #week {
border-top: 3px solid #ccc;
border-bottom: 3px solid #ddd;
background: #eee;
height: auto;
voice-family: "\"}\"";
voice-family: inherit;
height: 100%;
}
.Issac{}
#calendar #week li {
float: left;
padding: 3px 0px;
border-right: 1px solid #eee;
border-bottom: 3px solid #999;
font: 9px Arial, Helvetica, sans-serif;
text-transform: uppercase;
width: 26px;
voice-family: "\"}\"";
voice-family: inherit;
width: 25px;
}
.Issac{}
#calendar #week .sun {
border-left: 1px solid #eee;
color: #f00;
}
#calendar #week .sat {
color: #f60;
}
/*日历日期*/
#calendar #day {
clear: both;
}
#calendar #day li {
float: left;
padding: 3px 0px;
border-right: 1px solid #ddd;
border-bottom: 1px solid #ddd;
width: 26px;
voice-family: "\"}\"";
voice-family: inherit;
width: 25px;
}
.Issac{}
#calendar #day .normal a {
color: #666;
}
#calendar #day .theSun {
border-left: 1px solid #ddd;
}
#calendar #day .theSun a{
color: #f00;
}
#calendar #day .theSat a{
color: #f60;
}
#calendar #day #now {
background: #f90;
}
#calendar #day #now a {
color: #fff;
}
#calendar #day .otherDay a {
color: #ccc;
}
#calendar #day .otherSun{
border-left: 1px solid #ddd;
}
#calendar #day .otherSun a{
color: #ccc;
}
/*日历操作(今天&取消)*/
#calendar #option {
clear: both;
height: 24px;
border-top: 2px solid #eee;
border-bottom: 3px solid #eee;
background: #f5f5f5;
line-height: 24px;
}
#calendar #option a {
margin: 0px 2px;
padding: 1px 3px 0px;
color: #333;
}
#calendar #option a:hover {
color: #f60;
border-bottom: 1px dashed #f60;
}
分享到:
评论

相关推荐

    javascript 创建日历控件

    用javascript创建日历控件 可以在html 、asp.net中应用

    利用js和css创建日历控件

    使用css和javascript技术实现日历控件,可以更好的对日历的日期进行控制

    可选择带时间的日历控件

    该日历控件可以选择只有日历、日历带时间,还可以选择是否换肤色(需要修改一下js中顶部的全局变量,很简单的)。

    日历控件需要引入的js和css包

    日历控件,加载后可以出现日历控件,可以选中天进行备注信息的填写

    js 日历控件

    controlId: "divDate", // 弹出的日期控件ID,默认: $(this).attr("id") + "Calendar" speed: 200, // 三种预定速度之一的字符串("slow", "normal", or "fast")或表示动画时长的毫秒数值(如:1000),默认:200 ...

    [原创]javascript 日历+日程控件

    纯javascript写的周历控件,加入了日程管理,并且类似于OutLook的周历功能,精确到小时。类似于现实中的台历功能.可以应用到不同语言开发的项目中,并且留有显示数据的接口.界面美观大方,可直接使用.

    ASP.NET中日历控件和JS版日历控件的使用方法(第5节)

    2、设计一个注册页面,使用js日历控件帮助用户输入出生日期。效果如图所示: 学习项目一 Calendar日历控件 1、在站点下创建一个Calendar页面,并在页面上拖放一个TextBox控件用来输入日期,一个Calendar1日历控件...

    用Javascript设计通用日历控件及其与ASP的结合应用

    1.1.1 制作通用日历控件的意义 1 1.1.2 使用JavaScript的必要性 1 1.2 相关技术 2 1.2.1 JavaScript 2 1.2.2 Asp 3 1.2.3 结构化查询语言——SQL 4 第二章 通用日历控件的功能分析 5 2.1 按需显示 5 2.2 多日期选择...

    日历控件使用的详细解释

    1.将文件下载到本地,然后在项目的根目录下新建js文件夹,将下载的Calendar.js文件放...&lt;SCRIPT language=Javascript src="../../js/Calendar.js"&gt;(路径自己进行修改) 3.开始创建Calendar进行使用。().show(this);"&gt;

    iCal-like Calendar 日历控件

    超酷日历控件,利用jQuery+CSS创建,可以添加到自己网站上。

    ASP.NET 控件的使用

    3.1.1 验证控件与JavaScript 85 3.1.2 使用Page.IsValid 85 3.1.3 设置Display属性 86 3.1.4 突出显示验证错误 86 3.1.5 使用验证组 90 3.1.6 禁用验证 93 3.2 使用RequiredFieldValidator控件 94 3.3 使用...

    jQuery控件简易日历表格代码.zip

    jQuery控件简易日历表格代码是一款较为简单的显示日历的代码,兼容firefox、chrome、ie。

    simplecalendar:简单的日历测试用例

    但是,可以创建会生成此类标签/输入的速记。日历作为单身人士我选择采用单例风格的日历行为方法。 一次不可能显示多个日历弹出窗口:我认为这种行为对用户来说是清晰直观的; 多个弹出窗口可能会使屏幕发出咔嗒声。...

    jQuery日历插件cal-heatmap.zip

    Cal-Heatmap 是个 javascript 模块,用来创建日历 heatmap 来查看时间序列数据。这个模块会帮助用户创建一个日历,就像 github 用户贡献日历一样,但是 Cal-Heatmap 包括了导航和更多的数据格式控件。 标签...

    WdatePicker日期插件框架资源包4.8.5

    日历控件:WdatePicker提供一个日历控件,可以在网页中方便地选择日期。用户可以通过单击或手动输入日期来选择所需的日期。 丰富的选择功能:WdatePicker支持多种日期选择方式,例如年、月、日、时、分的选择,还...

    My97DatePicker JS日历插件

    [新增]当使用onfocus触发时,焦点在日期控件时,也会弹出日期控件 [新增]autoUpdateOnChanged属性:在修改年月日时分秒等元素时是否自动更新到el [修正]FF3.5细微的定位偏差问题 [修正]在双月模式时,可以隐藏其他月份的...

    ASP.NET.4揭秘

    3.1.1 验证控件与javascript93 3.1.2 使用page.isvalid94 3.1.3 设置display属性94 3.1.4 突出显示验证错误95 3.1.5 使用验证组99 3.1.6 禁用验证103 3.2 使用requiredfieldvalidator控件104 3.3 使用rangevalidator...

    菜单树(树数据结构+JSP页面递归调用构建菜单树)

    菜单树(树数据结构+JSP页面递归调用构建菜单树)

    为开发者准备的10款最好的jQuery日历插件

    这篇文章介绍的是 10 ... CLNDR.js 是一个日历插件,用来创建日历,允许用户随意的按照自己的想法去自定义日历。这个插件不会生成任何的标记,但是可以使用数据来填充 Underscore.js HTML 模版(产生下划线)。它提供

    程序天下:JavaScript实例自学手册

    14.3 JavaScript创建二维数组 14.4 截断小数点位数 14.5 删除数组中指定元素 14.6 数字选中后放大 14.7 统计字符数的方法 14.8 JavaScript遍历数组 14.9 获取字符串型数组下标的数组长度 14.10 用JavaScript实现数组...

Global site tag (gtag.js) - Google Analytics