JS制作简单的日历控件
JS制作简单的日历控件【JS Date对象练习实例】
by jquery2005
HTML
<br/>
<br/>
<h3>支持选择年月 支持SELECT选择年月</h3>
<div><input type="text" id="j_Date1" class="input"> <input type="text" id="j_Date2" class="input"></div>
<br/>
<div></div>
CSS
.calendar{font-family:Tahoma; background:#fff; float:left; border-style:solid; border-width:1px; border-color:#85BEE5 #3485C0 #3485C0 #85BEE5; position:relative; padding:10px; overflow:hidden;}
.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;}
.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;}
JavaScript
<!--日历控件JS源码-->
<!--<script>-->
/**
* @namespace _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;
}
};
/**
*...