一个纯Javascript脚本的datepicker

if(typeof(mksword) == "undefined"){
	mksword = new Object();
}

mksword.DatePicker = function(id){
	this.Base = document.getElementById(id);
	this.PromptBase = document.createElement("div");
	this.PromptBase.style.position = "absolute";
	this.PromptBase.style.top = this.getPos(this.Base).y + 21;
	this.PromptBase.style.left = this.getPos(this.Base).x;
	this.PromptBase.style.border = "1px Black solid";
	this.PromptBase.style.backgroundColor="#FFCC80";
	this.PromptBase.style.visibility = "hidden";
	this.PromptBase.style.zIndex = "99";
	this.PromptBase.id = id + "_list";
	document.body.appendChild(this.PromptBase);
	var d = new Date();
	this.Year = d.getFullYear();
	this.Month = d.getMonth();
	this.Date = d.getDate();
	this.Id = id + "_Picker";
	window[this.Id] = this;
	var ref = this;
	this.Base.onfocus = function(){
		ref.showCalendar();
	}
	this.showDate();
}

mksword.DatePicker.prototype.Base;

mksword.DatePicker.prototype.PromptBase;

mksword.DatePicker.prototype.Id;

mksword.DatePicker.prototype.getPos = function(el){
	for(var pos = {x:0,y:0};el;el = el.offsetParent){
		pos.y += el.offsetTop;
		pos.x += el.offsetLeft;
	}
	return pos;
}

mksword.DatePicker.prototype.Year;
mksword.DatePicker.prototype.Month;
mksword.DatePicker.prototype.Date;

mksword.DatePicker.prototype.showDate = function(){	
	this.Base.value = this.Year+"年"+(this.Month+1)+"月"+this.Date+"日";
	this.PromptBase.style.visibility = "hidden";
}

mksword.DatePicker.prototype.showCalendar = function(){
	var str = "<div><span οnclick='"+this.Id+".PY()'><<</span> <span οnclick='"+this.Id+".PM()'><</span>"
		+" <input id='"+this.Id+"_Lb"+"' type='text' value='"+this.Year+"年"+(this.Month+1)+"月"+this.Date+"日"
		+"'/> <span οnclick='"+this.Id+".NM()'>></span> <span οnclick='"+this.Id+".NY()'>>></span></div>"
		+"<div id='"+this.Id+"_DatePane"+"'><table cellpadding=\"0\" cellspacing=\"0\" style=\"font-size:18pt;width:240px\"><tr><th><font color='red'>日</font></th><th>一</th><th>二</th><th>三</th><th>四</th>"
		+"<th>五</th><th><font color='red'>六</font></th></tr>";
	var con = [];
	var d1 = new Date();
	d1.setFullYear(this.Year,this.Month,1);
	var v1 = d1.getDay();
	for(var wb = 0; wb < v1; wb++)
		con.push(" ");
	var d2 = new Date();
	d2.setFullYear(this.Year,this.Month + 1,1);
	var v2 = (d2 - d1)/ 86400000;
	for(var db = 0; db < v2; db++){
		con.push("<span οnclick='"+this.Id+".Selected("+(db+1)+")'>"+(db+1)+"</span>");
	}
	var tend = con.length % 7;
	if(tend != 0)
		for(var td = 0;td < 7 - tend;td++)
			con.push(" ");
	var std = 0;
	for(var itr = 0;itr < con.length; itr++){
		if(itr == 0)
			str += "<tr><td><font color='red'>"+con[itr]+"</font></td>";
		else if(itr%7 == 0)
			str += "</tr><tr><td><font color='red'>"+con[itr]+"</font></td>";
		else if((itr - std*7)%6 == 0 && itr > 0){
			str += "<td><font color='red'>"+con[itr]+"</font></td>";
			std++;
		}
		else if(itr == con.length - 1)
			str += "</tr>";
		else
			str += "<td>"+con[itr]+"</td>";
	} 
	str += "</table></div>"; 
	this.PromptBase.innerHTML = str;
	this.PromptBase.style.visibility = "visible";
}

mksword.DatePicker.prototype.UpdateLabel = function(){
	var lb = document.getElementById(this.Id+"_Lb");
	lb.value = this.Year+"年"+(this.Month+1)+"月"+this.Date+"日";
	this.showCalendar();
}

mksword.DatePicker.prototype.Selected = function(date){
	this.Date = date;
	this.showDate();
	this.PromptBase.style.visibility = "hidden";
}

mksword.DatePicker.prototype.PY = function(){
	this.Year--;	
	this.showDate();
	this.UpdateLabel();
}

mksword.DatePicker.prototype.PM = function(){
	this.Month--;	
	this.showDate();
	this.UpdateLabel();
}

mksword.DatePicker.prototype.NY = function(){
	this.Year++;	
	this.showDate();
	this.UpdateLabel();
}

mksword.DatePicker.prototype.NM = function(){
	this.Month++;	
	this.showDate();
	this.UpdateLabel();
}


这是一个纯脚本的datepicker

用法是

<input id="test" type="text"/>
<script language="javascript" type="text/javascript">
var x = new mksword.DatePicker("test");
</script>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值