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 相关文章推荐
jQuery 使用手册(一)
Sep 23 Javascript
jquery1.4后 jqDrag 拖动 不可用
Feb 06 Javascript
js select常用操作控制代码
Mar 16 Javascript
Google排名中的10个最著名的 JavaScript库
Apr 27 Javascript
js实现同一页面多个不同运动效果的方法
Apr 10 Javascript
Javascript基于jQuery UI实现选中区域拖拽效果
Nov 25 Javascript
基于javascript的Form表单验证
Dec 29 Javascript
浅谈vue-cli 3.0.x 初体验
Apr 11 Javascript
Vue数据双向绑定的深入探究
Nov 27 Javascript
微信小程序实现单列下拉菜单效果
Apr 25 Javascript
了解Javascript中函数作为对象的魅力
Jun 19 Javascript
Vue3新特性之在Composition API中使用CSS Modules
Jul 13 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
在IIS上安装PHP4.0正式版
2006/10/09 PHP
PHP字符编码问题之GB2312 VS UTF-8解决方法
2011/06/23 PHP
phpstudy隐藏index.php的方法
2020/09/21 PHP
Mootools 1.2教程 Fx.Tween的使用
2009/09/15 Javascript
javascript内存管理详细解析
2013/11/11 Javascript
超链接的禁用属性Disabled使用示例
2014/07/31 Javascript
深入分析JSONP跨域的原理
2014/12/10 Javascript
JavaScript学习笔记之基础语法
2015/01/22 Javascript
js实现按钮颜色渐变动画效果
2015/08/20 Javascript
js将json格式的对象拼接成复杂的url参数方法
2016/05/25 Javascript
jQuery EasyUI 获取tabs的实例解析
2016/12/06 Javascript
angular.js 路由及页面传参示例
2017/02/24 Javascript
基于jQuery实现的Ajax 验证用户名唯一性实例代码
2017/06/28 jQuery
bootstrap table实现点击翻页功能 可记录上下页选中的行
2017/09/28 Javascript
laydate日历控件使用方法详解
2017/11/20 Javascript
利用ES6实现单例模式及其应用详解
2017/12/09 Javascript
JavaScript寄生组合式继承实例详解
2018/01/06 Javascript
vue项目中api接口管理总结
2018/04/20 Javascript
layui的layedit富文本赋值方法
2019/09/18 Javascript
JS的时间格式化和时间戳转换函数示例详解
2020/07/27 Javascript
详解微信小程序「渲染层网络层错误」的解决方法
2021/01/06 Javascript
Python标准库urllib2的一些使用细节总结
2015/03/16 Python
实例讲解Python中global语句下全局变量的值的修改
2016/06/16 Python
matplotlib设置legend图例代码示例
2017/12/19 Python
PyQt5 加载图片和文本文件的实例
2019/06/14 Python
Python面向对象封装操作案例详解
2019/12/31 Python
python序列类型种类详解
2020/02/26 Python
Django中的AutoField字段使用
2020/05/18 Python
用python读取xlsx文件
2020/12/17 Python
CSS中几个与换行有关的属性简明总结
2014/04/15 HTML / CSS
新领导上任欢迎词
2014/01/13 职场文书
单位介绍信范文
2014/01/18 职场文书
学员自我鉴定
2014/03/19 职场文书
务虚会发言材料
2014/12/25 职场文书
2015年出纳年终工作总结
2015/05/14 职场文书
欢送领导祝酒词
2015/08/12 职场文书