javascript 小日历

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="日历.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     <meta charset="utf-8">     <title>日历组件示例</title>     <style>         .calendar         {             font-family: Tahoma;             background: #fff;             float: left;             border-style: solid;             border-width: 1px;             border-color: #85BEE5 #3485C0 #3485C0 #85BEE5;             position: relative;             padding: 10px;         }         .calendar dl, .calendar dd         {             margin: 0;             padding: 0;             width: 183px;             font-size: 12px;             line-height: 22px;         }         .calendar dt.title-date         {             display: block;             border-bottom: 1px solid #E4E4E4;             font-weight: 700;             position: relative;             margin-bottom: 5px;             padding-bottom: 3px;         }         .calendar dt         {             float: left;             width: 25px;             margin-left: 1px;             text-align: center;         }         .calendar dt.title-date         {             width: 100%;         }         .calendar dd         {             clear: both;             width: 183px;             height: 139px;             font-weight: 700;             background: url(http://images.cnblogs.com/cnblogs_com/NNUF/379856/o_bg.png) no-repeat;             margin: 0;         }         .prevyear, .nextyear, .prevmonth, .nextmonth         {             cursor: pointer;             height: 9px;             background: url(http://images.cnblogs.com/cnblogs_com/NNUF/379856/o_nextprv.png) no-repeat;             overflow: hidden;             position: absolute;             top: 8px;             text-indent: -999px;         }         .prevyear         {             left: 4px;             width: 9px;         }         .prevmonth         {             width: 5px;             background-position: -9px 0;             left: 20px;         }         .nextyear         {             width: 9px;             background-position: -19px 0;             right: 5px;         }         .nextmonth         {             width: 5px;             background-position: -14px 0;             right: 20px;         }         .calendar dd a         {             float: left;             width: 25px;             height: 22px;             color: blue;             overflow: hidden;             text-decoration: none;             margin: 1px 0 0 1px;             text-align: center;         }         .calendar dd a.disabled         {             color: #999;         }         .calendar dd a.tody         {             color: red;         }         .calendar dd a.on         {             background: blue;             color: #fff;         }         .calendar dd a.live         {             cursor: pointer;         }         .input         {             border: 1px solid #ccc;             padding: 4px;             background: url(http://images.cnblogs.com/cnblogs_com/NNUF/379856/o_nextprv.png) no-repeat right -18px;         }     </style>     <script type="text/javascript" language="javascript">         /** 日历控件所用便捷函数 _CalF  * */         _CalF = {             // 选择元素             $: function (arg, context) {                 var tagAll, n, eles = [], i, sub = arg.substring(1);                 context = context || document;                 if (typeof arg == ‘string‘) {                     switch (arg.charAt(0)) {                         case ‘#‘:                             return document.getElementById(sub);                             break;                         case ‘.‘:                             if (context.getElementsByClassName) return context.getElementsByClassName(sub);                             tagAll = _CalF.$(‘*‘, context);                             n = tagAll.length;                             for (i = 0; i < n; i++) {                                 if (tagAll[i].className.indexOf(sub) > -1) eles.push(tagAll[i]);                             }                             return eles;                             break;                         default:                             return context.getElementsByTagName(arg);                             break;                     }                 }             },             // 绑定事件             bind: function (node, type, handler) {                 node.addEventListener ? node.addEventListener(type, handler, false) : node.attachEvent(‘on‘ + type, handler);             },             // 获取元素位置             getPos: function (node) {                 var scrollx = document.documentElement.scrollLeft || document.body.scrollLeft,                 scrollt = document.documentElement.scrollTop || document.body.scrollTop;                 pos = node.getBoundingClientRect();                 return { top: pos.top + scrollt, right: pos.right + scrollx, bottom: pos.bottom + scrollt, left: pos.left + scrollx }             },             // 添加样式名             addClass: function (c, node) {                 node.className = node.className + ‘ ‘ + c;             },             // 移除样式名             removeClass: function (c, node) {                 var reg = new RegExp("(^|\\s+)" + c + "(\\s+|$)", "g");                 node.className = node.className.replace(reg, ‘‘);             },             // 阻止冒泡             stopPropagation: function (event) {                 event = event || window.event;                 event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true;             }         };         /**         * @name Calender         * @constructor         * */         function Calender() {             this.initialize.apply(this, arguments);         }         Calender.prototype = {             constructor: Calender,             // 模板数组             _template: [         ‘<dl>‘,         ‘<dt class="title-date">‘,         ‘<span class="prevyear">prevyear</span><span class="prevmonth">prevmonth</span>‘,         ‘<span class="nextyear">nextyear</span><span class="nextmonth">nextmonth</span>‘,         ‘</dt>‘,         ‘<dt><strong>日</strong></dt>‘,         ‘<dt>一</dt>‘,         ‘<dt>二</dt>‘,         ‘<dt>三</dt>‘,         ‘<dt>四</dt>‘,         ‘<dt>五</dt>‘,         ‘<dt><strong>六</strong></dt>‘,         ‘<dd></dd>‘,         ‘</dl>‘],             // 初始化对象             initialize: function (options) {                 this.id = options.id; // input的ID                 this.input = _CalF.$(‘#‘ + this.id); // 获取INPUT元素                 this.isSelect = options.isSelect;   // 是否支持下拉SELECT选择年月,默认不显示                 this.inputEvent(); // input的事件绑定,获取焦点事件             },             // 创建日期最外层盒子,并设置盒子的绝对定位             createContainer: function () {                 // 如果存在,则移除整个日期层Container                 var odiv = _CalF.$(‘#‘ + this.id + ‘-date‘);                 if (!!odiv) odiv.parentNode.removeChild(odiv);                 var container = this.container = document.createElement(‘div‘);                 container.id = this.id + ‘-date‘;                 container.style.position = "absolute";                 container.zIndex = 999;                 // 获取input表单位置inputPos                 var input = _CalF.$(‘#‘ + this.id),                 inputPos = _CalF.getPos(input);                 // 根据input的位置设置container高度                 container.style.left = inputPos.left + ‘px‘;                 container.style.top = inputPos.bottom - 1 + ‘px‘;                 // 设置日期层上的单击事件,仅供阻止冒泡,用途在日期层外单击关闭日期层                 _CalF.bind(container, ‘click‘, _CalF.stopPropagation);                 document.body.appendChild(container);             },             // 渲染日期             drawDate: function (odate) { // 参数 odate 为日期对象格式                 var dateWarp, titleDate, dd, year, month, date, days, weekStart, i, l, ddHtml = [], textNode;                 var nowDate = new Date(), nowyear = nowDate.getFullYear(), nowmonth = nowDate.getMonth(),                 nowdate = nowDate.getDate();                 this.dateWarp = dateWarp = document.createElement(‘div‘);                 dateWarp.className = ‘calendar‘;                 dateWarp.innerHTML = this._template.join(‘‘);                 this.year = year = odate.getFullYear();                 this.month = month = odate.getMonth() + 1;                 this.date = date = odate.getDate();                 this.titleDate = titleDate = _CalF.$(‘.title-date‘, dateWarp)[0];                 // 是否显示SELECT                 if (this.isSelect) {                     var selectHtmls = [];                     selectHtmls.push(‘<select>‘);                     for (i = 2020; i > 1970; i--) {                         if (i != this.year) {                             selectHtmls.push(‘<option value ="‘ + i + ‘">‘ + i + ‘</option>‘);                         } else {                             selectHtmls.push(‘<option value ="‘ + i + ‘" selected>‘ + i + ‘</option>‘);                         }                     }                     selectHtmls.push(‘</select>‘);                     selectHtmls.push(‘年‘);                     selectHtmls.push(‘<select>‘);                     for (i = 1; i < 13; i++) {                         if (i != this.month) {                             selectHtmls.push(‘<option value ="‘ + i + ‘">‘ + i + ‘</option>‘);                         } else {                             selectHtmls.push(‘<option value ="‘ + i + ‘" selected>‘ + i + ‘</option>‘);                         }                     }                     selectHtmls.push(‘</select>‘);                     selectHtmls.push(‘月‘);                     titleDate.innerHTML = selectHtmls.join(‘‘);                     // 绑定change事件                     this.selectChange();                 } else {                     textNode = document.createTextNode(year + ‘年‘ + month + ‘月‘);                     titleDate.appendChild(textNode);                     this.btnEvent();                 }                 // 获取模板中唯一的DD元素                 this.dd = dd = _CalF.$(‘dd‘, dateWarp)[0];                 // 获取本月天数                 days = new Date(year, month, 0).getDate();                 // 获取本月第一天是星期几                 weekStart = new Date(year, month - 1, 1).getDay();                 // 开头显示空白段                 for (i = 0; i < weekStart; i++) {                     ddHtml.push(‘<a>&nbsp;</a>‘);                 }                 // 循环显示日期                 for (i = 1; i <= days; i++) {                     if (year < nowyear) {                         ddHtml.push(‘<a class="live disabled">‘ + i + ‘</a>‘);                     } else if (year == nowyear) {                         if (month < nowmonth + 1) {                             ddHtml.push(‘<a class="live disabled">‘ + i + ‘</a>‘);                         } else if (month == nowmonth + 1) {                             if (i < nowdate) ddHtml.push(‘<a class="live disabled">‘ + i + ‘</a>‘);                             if (i == nowdate) ddHtml.push(‘<a class="live tody">‘ + i + ‘</a>‘);                             if (i > nowdate) ddHtml.push(‘<a class="live">‘ + i + ‘</a>‘);                         } else if (month > nowmonth + 1) {                             ddHtml.push(‘<a class="live">‘ + i + ‘</a>‘);                         }                     } else if (year > nowyear) {                         ddHtml.push(‘<a class="live">‘ + i + ‘</a>‘);                     }                 }                 dd.innerHTML = ddHtml.join(‘‘);

// 如果存在,则先移除                 this.removeDate();                 // 添加                 this.container.appendChild(dateWarp);

//IE6 select遮罩                 var ie6 = !!window.ActiveXObject && !window.XMLHttpRequest;                 if (ie6) dateWarp.appendChild(this.createIframe());

// A link事件绑定                 this.linkOn();                 // 区域外事件绑定                 this.outClick();             },

createIframe: function () {                 var myIframe = document.createElement(‘iframe‘);                 myIframe.src = ‘about:blank‘;                 myIframe.style.position = ‘absolute‘;                 myIframe.style.zIndex = ‘-1‘;                 myIframe.style.left = ‘-1px‘;                 myIframe.style.top = 0;                 myIframe.style.border = 0;                 myIframe.style.filter = ‘alpha(opacity= 0 )‘;                 myIframe.style.width = this.container.offsetWidth + ‘px‘;                 myIframe.style.height = this.container.offsetHeight + ‘px‘;                 return myIframe;

},

// SELECT CHANGE 事件             selectChange: function () {                 var selects, yearSelect, monthSelect, that = this;                 selects = _CalF.$(‘select‘, this.titleDate);                 yearSelect = selects[0];                 monthSelect = selects[1];                 _CalF.bind(yearSelect, ‘change‘, function () {                     var year = yearSelect.value;                     var month = monthSelect.value;                     that.drawDate(new Date(year, month - 1, that.date));                 });

_CalF.bind(monthSelect, ‘change‘, function () {                     var year = yearSelect.value;                     var month = monthSelect.value;                     that.drawDate(new Date(year, month - 1, that.date));                 })             },             // 移除日期DIV.calendar             removeDate: function () {                 var odiv = _CalF.$(‘.calendar‘, this.container)[0];                 if (!!odiv) this.container.removeChild(odiv);             },             // 上一月,下一月按钮事件             btnEvent: function () {                 var prevyear = _CalF.$(‘.prevyear‘, this.dateWarp)[0],                 prevmonth = _CalF.$(‘.prevmonth‘, this.dateWarp)[0],                 nextyear = _CalF.$(‘.nextyear‘, this.dateWarp)[0],                 nextmonth = _CalF.$(‘.nextmonth‘, this.dateWarp)[0],                 that = this;                 prevyear.onclick = function () {                     var idate = new Date(that.year - 1, that.month - 1, that.date);                     that.drawDate(idate);                 };                 prevmonth.onclick = function () {                     var idate = new Date(that.year, that.month - 2, that.date);                     that.drawDate(idate);                 };                 nextyear.onclick = function () {                     var idate = new Date(that.year + 1, that.month - 1, that.date);                     that.drawDate(idate);                 };                 nextmonth.onclick = function () {                     var idate = new Date(that.year, that.month, that.date);                     that.drawDate(idate);                 }             },             // A 的事件             linkOn: function () {                 var links = _CalF.$(‘.live‘, this.dd), i, l = links.length, that = this;                 for (i = 0; i < l; i++) {                     links[i].index = i;                     links[i].onmouseover = function () {                         _CalF.addClass("on", links[this.index]);                     };                     links[i].onmouseout = function () {                         _CalF.removeClass("on", links[this.index]);                     };                     links[i].onclick = function () {                         that.date = this.innerHTML;                         that.input.value = that.year + ‘-‘ + that.month + ‘-‘ + that.date;                         that.removeDate();                     }                 }             },             // 表单的事件             inputEvent: function () {                 var that = this;                 _CalF.bind(this.input, ‘focus‘, function () {                     that.createContainer();                     that.drawDate(new Date());                 });             },             // 鼠标在对象区域外点击,移除日期层             outClick: function () {                 var that = this;                 _CalF.bind(document, ‘click‘, function (event) {                     event = event || window.event;                     var target = event.target || event.srcElement;                     if (target == that.input) return;                     that.removeDate();                 })             }         };         //        var myDate1 = new Calender({ id: ‘j_Date1‘ });               </script> </head> <body>     <div>         <center>             <h3>                 简单的日历</h3>             <div>                 <input type="text" id="j_Date" class="input"/></div>         </center>     </div>     <!--日历初始化-->     <script type="text/javascript" language="javascript">         var myDate2 = new Calender({ id: ‘j_Date‘, isSelect: !0 });     </script> </body> </html>

时间: 2024-10-11 22:36:44

javascript 小日历的相关文章

Linux下之使用简单3种创建文件的命令,并实现一个Html和JavaScript小程序

1.今天来说一下在linux下如何实现一个JavaScript程序,这个很简单,如果大家想要在linux下搞web开发,服务器架设,大数据等方面,就得继续努力了! 2.首先,我们要实现一个JavaScript小程序,必须要有一个后缀名为html.htm等的文件,所以我先介绍一下三个创建文件的命令: (1).touch命令:这个命令可以在当前工作目录底下新建一个文件,示例如下图: 这个touch命令只创建了一个空文件,要输入内容又必须使用gedit命令和vi命令来打开文件,并可以写入内容,如果文件

JavaScript简易日历

<!DOCTYPE html PUBLIC "-//W3C//h2D XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/h2D/xhtml1-transitional.h2d"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

JavaScript小技巧

1.变量转换 看起来很简单,但据我所看到的,使用构造函数,像Array()或者Number()来进行变量转换是常用的做法.始终使用原始数据类型(有时也称为字面量)来转换变量,这种没有任何额外的影响的做法反而效率更高. 1 2 3 4 5 6 7 var myVar   = "3.14159", str     = ""+ myVar,//  to string int     = ~~myVar,  //  to integer float   = 1*myVar,

javascript 小例子(待补充)

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script> function figure1(){ var figure = money.value; var wushi = Math.floor(figure/50); var ershi = Math

考考你!一道有趣的Javascript小题目

今天的内容很简单,给大家分享一个有趣的Javascript小题目. 题目很简单,就是填空: var a = ______; var b = a;alert(a==b); // alert "false" 请将程序补充完整,使得弹出的对话框显示为"false". 先答出的有奖哦 ^ ^ --------------------- 用简单而风趣的形式表达出自己的想法是我一直追求的目标(当然,目前还处于"XX主义初级阶段",还有很长的路要走). 如果你

javascript小括号、中括号、大括号学习总结

作为一名编程人员,和括号打交道是必不可少的.你可知道在不同的上下文中,括号的作用是不一样的,今天就让我们简单总结下javascript小括号.中括号.大括号的用法. 总的来说,JavaScript中小括号有五种语义,中括号有四种语义,而大括号也有四种语义. 小括号: 语义1,函数声明时参数表(形参) 语义2,和一些语句联合使用以达到某些限定作用(if.for等) 语义3,和new一起使用用来传值(实参) 语义4,作为函数或对象方法的调用运算符(如果定义了参数也可与语义3一样传实参) 语义5,强制

常用的javascript小技巧

字符串转换为数值 常规方法: var var1 = parseInt("2"); var var2 = parseFloat("2"); var var3 = Number("2"); var var3 = new Number("2"); 简便方法: var var1 = +("2"); 将其他类型转换为boolean类型 在JavaScript中,所有值都能隐式的转化为Boolean类型: 数据类型 转

javascript小技巧之with()方法

With()方法平时用得不多,本文用个小例子来学习一下.在这里记录.个人感觉还是很方便的. 有了 With 语句,在存取对象属性和方法时就不用重复指定参考对象,在 With 语句块中,凡是 JavaScript 不识别的属性和方法都和该语句块指定的对象有关.With 语句的语法格式如下所示:With Object {Statements}对象指明了当语句组中对象缺省时的参考对象,这里我们用较为熟悉的 Document 对象对 With 语句举例.例如 当使用与 Document 对象有关的 wr

12个非常实用的JavaScript小技巧

在这篇文章中将给大家分享12个有关于JavaScript的小技巧.这些小技巧可能在你的实际工作中或许能帮助你解决一些问题. 使用!!操作符转换布尔值 有时候我们需要对一个变量查检其是否存在或者检查值是否有一个有效值,如果存在就返回true值.为了做这样的验证,我们可以使用!!操作符来实现是非常的方便与简单.对于变量可以使用!!variable做检测,只要变量的值为:0.null." ".undefined或者NaN都将返回的是false,反之返回的是true.比如下面的示例: func