JavaScript实现的日期控件具体代码


Posted in Javascript onNovember 18, 2013
<html> 
<head> 
<style> 
<!-- 
.wr{font-size: 12pt; line-height: 22px} 
.wr1 {  FONT-SIZE: 12px; LINE-HEIGHT: 200%} 
.wr2 {  FONT-SIZE: 14px; LINE-HEIGHT: 200%} 
.wr3 {  FONT-SIZE: 12px} 
.wr4 {  FONT-SIZE: 12px; LINE-HEIGHT: 150%} 
// --> 
</style> <title>日期自动输入控件</title> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
</head> 
<style type="text/css"> 
.date-picker-wp { 
display: none; 
position: absolute; 
background: #f1f1f1; 
left: 40px; 
top: 40px; 
border-top: 4px solid #3879d9; 
} 
.date-picker-wp table { 
border: 1px solid #ddd; 
} 
.date-picker-wp td { 
background: #fafafa; 
width: 22px; 
height: 18px; 
border: 1px solid #ccc; 
font-size: 12px; 
text-align: center; 
} 
.date-picker-wp td.noborder { 
border: none; 
background: none; 
} 
.date-picker-wp td a { 
color: #1c93c4; 
text-decoration: none; 
} 
.strong {font-weight: bold} 
.hand {cursor: pointer; color: #3879d9} 
</style> 
<script type="text/javascript"> 
var DatePicker = function () { 
var $ = function (i)  
{ 
   return document.getElementById(i) 
}, 
addEvent = function (o, e, f) {o.addEventListener ? o.addEventListener(e, f, false) : o.attachEvent('on'+e, function(){f.call(o)})}, 
getPos = function (el) { 
for (var pos = {x:0, y:0}; el; el = el.offsetParent) { 
pos.x += el.offsetLeft; 
pos.y += el.offsetTop; 
} 
return pos; 
}; 
var init = function (n, config) { 
window[n] = this; 
Date.prototype._fd = function () {var d = new Date(this); d.setDate(1); return d.getDay()}; 
Date.prototype._fc = function () {var d1 = new Date(this), d2 = new Date(this); d1.setDate(1); d2.setDate(1); d2.setMonth(d2.getMonth()+1); return (d2-d1)/86400000;}; 
this.n = n; 
this.config = config; 
this.D = new Date; 
this.el = $(config.inputId); 
this.el.title = this.n+'DatePicker'; 
this.update(); 
this.bind(); 
}; 
init.prototype = { 
update : function (y, m) { 
var con = [], week = ['Su','Mo','Tu','We','Th','Fr','Sa'], D = this.D, _this = this; 
fn = function (a, b) {return '<td title="'+_this.n+'DatePicker" class="noborder hand" onclick="'+_this.n+'.update('+a+')">'+b+'</td>'}, 
_html = '<table cellpadding=0 cellspacing=2>'; 
y && D.setYear(D.getFullYear() + y); 
m && D.setMonth(D.getMonth() + m); 
var year = D.getFullYear(), month = D.getMonth() + 1, date = D.getDate(); 
for (var i=0; i<week.length; i++) con.push('<td title="'+this.n+'DatePicker" class="noborder">'+week[i]+'</td>'); 
for (var i=0; i<D._fd(); i++ ) con.push('<td title="'+this.n+'DatePicker" class="noborder"> </td>'); 
for (var i=0; i<D._fc(); i++ ) con.push('<td class="hand" onclick="'+this.n+'.fillInput('+year+', '+month+', '+(i+1)+')">'+(i+1)+'</td>'); 
var toend = con.length%7; 
if (toend != 0) for (var i=0; i<7-toend; i++) con.push('<td class="noborder"> </td>'); 
_html += '<tr>'+fn("-1, null", "<<")+fn("null, -1", "<")+'<td title="'+this.n+'DatePicker" colspan=3 class="strong">'+year+'/'+month+'/'+date+'</td>'+fn("null, 1", ">")+fn("1, null", ">>")+'</tr>'; 
for (var i=0; i<con.length; i++) _html += (i==0 ? '<tr>' : i%7==0 ? '</tr><tr>' : '') + con[i] + (i == con.length-1 ? '</tr>' : ''); 
!!this.box ? this.box.innerHTML = _html : this.createBox(_html); 
}, 
fillInput : function (y, m, d) { 
var s = this.config.seprator || '/'; 
this.el.value = y + s + m + s + d; 
this.box.style.display = 'none'; 
}, 
show : function () { 
var s = this.box.style, is = this.mask.style; 
s['left'] = is['left'] = getPos(this.el).x + 'px'; 
s['top'] = is['top'] = getPos(this.el).y + this.el.offsetHeight + 'px'; 
s['display'] = is['display'] = 'block'; 
is['width'] = this.box.offsetWidth - 2 + 'px'; 
is['height'] = this.box.offsetHeight - 2 + 'px'; 
}, 
hide : function () { 
this.box.style.display = 'none'; 
this.mask.style.display = 'none'; 
}, 
bind : function () { 
var _this = this; 
addEvent(document, 'click', function (e) { 
e = e || window.event; 
var t = e.target || e.srcElement; 
if (t.title != _this.n+'DatePicker') {_this.hide()} else {_this.show()} 
}); 
}, 
createBox : function (html) { 
var box = this.box = document.createElement('div'), mask = this.mask = document.createElement('iframe'); 
box.className = this.config.className || 'datepicker'; 
mask.src = 'javascript:false'; 
mask.frameBorder = 0; 
box.style.cssText = 'position:absolute;display:none;z-index:9999'; 
mask.style.cssText = 'position:absolute;display:none;z-index:9998'; 
box.title = this.n+'DatePicker'; 
box.innerHTML = html; 
document.body.appendChild(box); 
document.body.appendChild(mask); 
return box; 
} 
}; 
return init; 
}(); 
onload = function () { 
new DatePicker('_DatePicker_demo', { 
inputId: 'date-input', 
className: 'date-picker-wp', 
seprator: '-' 
}); 
} 
</script> 

<body bgcolor="#FFFFDB" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> 
<form > 
  <table border="0" width="60%" align="center"> 
   <tr> 
     <td width="45%" class="wr4" align="right"> 生日:</td> 
     <td width="55%" class="wr4"> 
         <input type="text" name="mtime" id="date-input">  <font color="RED">*</font>         
     </td> 
   </tr> 
  <tr>  
     <td width="45%" align="right"><input type = "submit" value = "确定"/></td>  
     <td width="55%"><input type = "reset" value = "重置"/></td>  
  </tr> 
</table> 
</form> 
</body> 
</html>

html页面中的js执行顺序:
1) 在head标签内的最先执行
2) 在body标签内的 执行
3) 当在 body标签中 加了 onload  事件时 对应的 js 最后执行,也就是当页面加载完在执行

注意:当在 body标签中 加了 onload  事件时 在head标签内,所引用外部的 js 不起作用,当换成 在body 内部或</html>之上引用外部js时可正常引用

Javascript 相关文章推荐
js身份证验证超强脚本
Oct 26 Javascript
js checkbox(复选框) 使用集锦
Apr 28 Javascript
jQueryUI的Dialog的简单封装
Jun 07 Javascript
jQuery UI Autocomplete 体验分享
Feb 14 Javascript
jquery实现图片渐变切换兼容ie6/Chrome/Firefox
Aug 02 Javascript
判断滚动条到底部的JS代码
Nov 04 Javascript
js获取触发事件元素在整个网页中的绝对坐标(示例代码)
Dec 13 Javascript
javascript判断chrome浏览器的方法
Mar 26 Javascript
Jquery焦点图实例代码
Nov 25 Javascript
js中使用使用原型(prototype)定义方法的好处详解
Jul 04 Javascript
深入学习 JavaScript中的函数调用
Mar 23 Javascript
switchery按钮的使用方法
Dec 18 Javascript
js获取当前月的第一天和最后一天的小例子
Nov 18 #Javascript
js向上无缝滚动,网站公告效果 具体代码
Nov 18 #Javascript
js call方法详细介绍(js 的继承)
Nov 18 #Javascript
jQuery操作Select的Option上下移动及移除添加等等
Nov 18 #Javascript
jQuery客户端分页实例代码
Nov 18 #Javascript
javascript验证身份证完全方法具体实现
Nov 18 #Javascript
Jquery通过Ajax访问XML数据的小例子
Nov 18 #Javascript
You might like
浅谈电磁辐射对健康的影响
2021/03/01 无线电
php和js交互一例-PHP教程,PHP应用
2007/01/03 PHP
解决PhpMyAdmin中导入2M以上大文件限制的方法分享
2014/06/06 PHP
destoon实现商铺管理主页设置增加新菜单的方法
2014/06/26 PHP
CodeIgniter错误mysql_connect(): No such file or directory解决方法
2014/09/06 PHP
基于PHP技术开发客服工单系统
2016/01/06 PHP
ThinkPHP 5 AJAX跨域请求头设置实现过程解析
2020/10/28 PHP
BOOM vs RR BO5 第一场 2.14
2021/03/10 DOTA
JavaScript 输入框内容格式验证代码
2010/02/11 Javascript
javascript实现一个数值加法函数
2015/06/26 Javascript
AngularJS ng-change 指令的详解及简单实例
2016/07/30 Javascript
js 判断数据类型的几种方法
2017/01/13 Javascript
原生JS实现自定义滚动条效果
2020/10/27 Javascript
jQuery实现鼠标响应式透明度渐变动画效果示例
2018/02/13 jQuery
vuex实现数据状态持久化
2019/11/11 Javascript
[03:49]2016完美“圣”典风云人物:AMS专访
2016/12/06 DOTA
[01:16:13]DOTA2-DPC中国联赛 正赛 SAG vs Dragon BO3 第一场 2月22日
2021/03/11 DOTA
Python smallseg分词用法实例分析
2015/05/28 Python
详解Python3中yield生成器的用法
2015/08/20 Python
详谈Pandas中iloc和loc以及ix的区别
2018/06/08 Python
使用python去除图片白色像素的实例
2019/12/12 Python
pandas中ix的使用详细讲解
2020/03/09 Python
如何搭建pytorch环境的方法步骤
2020/05/06 Python
h5调用摄像头的实现方法
2016/06/01 HTML / CSS
阿根廷票务网站:StubHub阿根廷
2018/04/13 全球购物
测试工程师岗位职责
2013/11/28 职场文书
遗体告别仪式答谢词
2014/01/23 职场文书
电视节目策划方案
2014/05/16 职场文书
学校学雷锋活动总结
2014/06/26 职场文书
三八活动策划方案
2014/08/17 职场文书
爱晚亭导游词
2015/02/09 职场文书
2015年全国“爱牙日”宣传活动总结
2015/03/23 职场文书
公司人事任命通知
2015/04/20 职场文书
《揠苗助长》教学反思
2016/02/20 职场文书
Python基础之进程详解
2021/05/21 Python
微软Win11 全新照片应用面向 Dev预览版推出 新版本上手体验图集
2022/09/23 数码科技