`

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创建的日历控件可以极大地增强用户体验,而My97DatePicker等库则提供了便捷的方式来实现这一功能,减少了开发时间和复杂性。通过深入理解和实践这些技术,你可以创建出符合需求的自定义日历组件...

    html js脚本日历控件

    创建日历控件首先需要在HTML中添加一个触发显示日历的按钮或者输入框,然后用JavaScript动态生成日历的HTML结构并插入到页面中。 2. **事件处理**:在JavaScript中,我们可以监听用户的点击、鼠标移动等事件,并...

    利用js和css创建日历控件

    ### 使用CSS和JavaScript创建日历控件的知识点详解 #### 一、项目概述 本项目旨在通过CSS和JavaScript实现一个可交互的日历控件。该控件不仅具有良好的视觉效果,还能让用户方便地选择日期,并在选中日期时提供反馈...

    js日历控件js日历控件

    - DOM操作:JavaScript通过Document Object Model(DOM)可以对HTML元素进行增删改查,创建日历控件通常需要动态生成HTML结构,如表格、按钮等。 - 事件处理:点击按钮显示/隐藏日历、选择日期等操作都需要绑定...

    js 日历控件 calendar 漂亮日历控件

    3. **JavaScript初始化**:在页面加载时,使用JavaScript初始化日历控件,这可能包括设置初始日期、创建日历UI元素以及添加事件监听器。 4. **事件处理**:当用户点击年份、月份选择器或日期单元格时,我们需要监听...

    js日历控件优化

    1. **性能优化**:在创建日历控件时,减少DOM操作是关键。利用虚拟DOM或仅在必要时更新DOM可以显著提升性能。此外,使用事件委托来处理用户交互,而非为每个元素绑定事件,可以降低内存消耗。 2. **响应式设计**:...

    JS常用日历控件合集

    - **自定义实现**:开发者可以利用JavaScript原生API创建日历,如DOM操作、事件处理等,从无到有构建整个日历界面和交互逻辑。 - **第三方库**:有很多成熟的JavaScript日历库,如`protocalendar-js-1.1.0`,它们...

    js日历控件 js日历控件

    总的来说,JavaScript日历控件是网页交互中不可或缺的一部分,它的实现涉及前端多个技术层面,从基础的HTML/CSS/JS到复杂的库使用和性能优化。理解其工作原理并灵活运用,能帮助开发者创建出更高效、更易用的网页...

    简单好看的js日历控件

    创建一个js日历控件,主要涉及以下几个知识点: 1. **DOM操作**:JavaScript通过Document Object Model(DOM)与HTML页面进行交互。你需要理解如何使用DOM API来创建、查找、修改和删除HTML元素。例如,创建一个...

    js日历控件 英文版

    开发者可能使用了`&lt;input type="text"&gt;`来创建一个日期输入字段,并通过JavaScript与`js.js`文件进行交互,当用户点击这个字段时,日历控件会弹出。 `js.js` 文件则是整个日历控件的核心,它包含了实现日历功能的...

    时间控件之JS 精美日历控件

    本文将围绕“JS精美日历控件”这一主题,详细介绍如何利用JavaScript实现一个功能丰富的日历插件,以My97DatePicker为例进行深入探讨。 My97DatePicker是一款广泛使用的JavaScript日历插件,它具有良好的兼容性,...

    JS日历控件,日历控件

    JS日历控件是利用JavaScript编程语言实现的此类功能,它能够提供直观、友好的用户体验。本文将深入探讨JS日历控件的原理、实现方式以及相关知识点。 一、日历控件的基本概念 日历控件是一种用户界面组件,它以日历...

    JavaScript日历控件 六种日历

    JavaScript日历控件是网页开发中常用的一种交互元素,它能帮助用户方便地选择日期,常见于表单输入、事件安排或时间相关的功能。在给定的资源中,包含了六种不同样式的JavaScript日历,这些日历可能具有不同的设计...

    js 日历控件

    4. 兼容性好:随着浏览器对JavaScript支持的增强,大多数现代浏览器都能良好运行JS日历控件。 5. 可扩展性:可以结合其他库如jQuery、React等,进一步增强功能,如添加时间选择、多选日期等。 三、应用场景 1. 表单...

    js 简易的日历控件

    创建一个JavaScript日历控件涉及到以下几个关键知识点: 1. **DOM操作**:日历控件需要动态地在网页上生成元素,这需要利用JavaScript对文档对象模型(DOM)进行操作。例如,可以使用`document.createElement()`...

    js日历 控件 集合

    综上所述,“js日历控件集合”涵盖了各种JavaScript实现的日历组件,每种都有其独特特性和应用场景。根据项目需求,选择合适且易于维护的控件至关重要。在实际开发中,应充分理解这些控件的工作原理和配置选项,以便...

    日历控件大集合—各式各样的日历控件

    从JavaScript实现到多样化功能,从用户体验到跨平台兼容性,理解并掌握日历控件的开发可以提升开发者在创建高效、用户友好的应用程序上的能力。通过学习和分析提供的文件,我们可以深入探究这些方面的具体实现和技巧...

    网页表单中的日历控件

    - JSP中,你可以使用Java的开源库,如`JQuery UI`的日期插件,或者`CalendarTag`库来创建日历控件。`JQuery UI`提供了一款美观易用的日历插件,只需引入对应的JavaScript和CSS文件,然后通过JavaScript代码调用来...

Global site tag (gtag.js) - Google Analytics