当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET中为TextBox中添加calendar.js示例代码

ASP.NET中为TextBox中添加calendar.js示例代码

2017年12月12日  | 移动技术网IT编程  | 我要评论
1.在工程文件下面新建文件夹"js",然后向其中添加javascript.js新建项,重命名为calendar.js。 2.向calendar.js中写入代码, 复制代码
1.在工程文件下面新建文件夹"js",然后向其中添加javascript.js新建项,重命名为calendar.js。

2.向calendar.js中写入代码,
复制代码 代码如下:

var cal;
var isfocus=false; //是否为焦点
//function selectdate(obj,strformat) //两个参数改为只传一个
function selectdate(obj)
{
var date = new date();
var by = date.getfullyear()-80; //最小值 → 80 年前
var ey = date.getfullyear()+20; //最大值 → 20 年后
cal = (cal==null) ? new calendar(by, ey, 0) : cal; //初始化为中文版,1为英文版
//cal.dateformatstyle = strformat; // 默认显示格式为:yyyy-mm-dd ,还可显示 yyyy/mm/dd
cal.show(obj);
}
/**//* 返回日期 */
string.prototype.todate = function(style){
var y = this.substring(style.indexof('y'),style.lastindexof('y')+1);//年
var m = this.substring(style.indexof('m'),style.lastindexof('m')+1);//月
var d = this.substring(style.indexof('d'),style.lastindexof('d')+1);//日
if(isnan(y)) y = new date().getfullyear();
if(isnan(m)) m = new date().getmonth();
if(isnan(d)) d = new date().getdate();
var dt ;
eval ("dt = new date('"+ y+"', '"+(m-1)+"','"+ d +"')");
return dt;
}
/**//* 格式化日期 */
date.prototype.format = function(style){
var o = {
"m+" : this.getmonth() + 1, //month
"d+" : this.getdate(), //day
"h+" : this.gethours(), //hour
"m+" : this.getminutes(), //minute
"s+" : this.getseconds(), //second
"w+" : "天一二三四五六".charat(this.getday()), //week
"q+" : math.floor((this.getmonth() + 3) / 3), //quarter
"s" : this.getmilliseconds() //millisecond
}
if(/(y+)/.test(style)){
style = style.replace(regexp.$1,
(this.getfullyear() + "").substr(4 - regexp.$1.length));
}
for(var k in o){
if(new regexp("("+ k +")").test(style)){
style = style.replace(regexp.$1,
regexp.$1.length == 1 ? o[k] :
("00" + o[k]).substr(("" + o[k]).length));
}
}
return style;
};

/**//*
* 日历类
* @param beginyear 2009
* @param endyear 2015
* @param lang 0(中文)|1(英语) 可自由扩充
* @param dateformatstyle "yyyy-mm-dd";
*/
function calendar(beginyear, endyear, lang, dateformatstyle){
this.beginyear = 2009;
this.endyear = 2015;
this.lang = 0; //0(中文) | 1(英文)
this.dateformatstyle = "yyyy-mm-dd";

if (beginyear != null && endyear != null){
this.beginyear = beginyear;
this.endyear = endyear;
}
if (lang != null){
this.lang = lang
}

if (dateformatstyle != null){
this.dateformatstyle = dateformatstyle
}

this.datecontrol = null;
this.panel = this.getelementbyid("calendarpanel");
this.container = this.getelementbyid("containerpanel");
this.form = null;

this.date = new date();
this.year = this.date.getfullyear();
this.month = this.date.getmonth();


this.colors = {
"cur_word" : "#ffffff", //当日日期文字颜色
"cur_bg" : "#83a6f4", //当日日期单元格背影色
"sel_bg" : "#ffcccc", //已被选择的日期单元格背影色
"sun_word" : "#ff0000", //星期天文字颜色
"sat_word" : "#0000ff", //星期六文字颜色
"td_word_light" : "#333333", //单元格文字颜色
"td_word_dark" : "#cccccc", //单元格文字暗色
"td_bg_out" : "#efefef", //单元格背影色
"td_bg_over" : "#ffcc00", //单元格背影色
"tr_word" : "#ffffff", //日历头文字颜色
"tr_bg" : "#666666", //日历头背影色
"input_border" : "#cccccc", //input控件的边框颜色
"input_bg" : "#efefef" //input控件的背影色
}

this.draw();
this.bindyear();
this.bindmonth();
this.changeselect();
this.binddata();
}

/**//*
* 日历类属性(语言包,可自由扩展)
*/
calendar.language ={
"year" : [[""], [""]],
"months" : [["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]
],
"weeks" : [["日","一","二","三","四","五","六"],
["sun","mon","tur","wed","thu","fri","sat"]
],
"clear" : [["清空"], ["cls"]],
"today" : [["今天"], ["today"]],
"close" : [["关闭"], ["close"]]
}

calendar.prototype.draw = function(){
calendar = this;

var mvary = [];
mvary[mvary.length] = ' <div name="calendarform" style="margin: 0px;">';
mvary[mvary.length] = ' <table width="100%" border="0" cellpadding="0" cellspacing="1">';
mvary[mvary.length] = ' <tr>';
mvary[mvary.length] = ' <th align="left" width="1%"><input style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:16px;height:20px;" name="prevmonth" type="button" id="prevmonth" value="<" /></th>';
mvary[mvary.length] = ' <th align="center"width="98%"><select name="calendaryear" id="calendaryear" style="width:48%;font-size:12px;"></select><select name="calendarmonth" id="calendarmonth" style="font-size:12px;width:48%;"></select></th>';
mvary[mvary.length] = ' <th align="right" width="1%"><input style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:16px;height:20px;" name="nextmonth" type="button" id="nextmonth" value=">" /></th>';
mvary[mvary.length] = ' </tr>';
mvary[mvary.length] = ' </table>';
mvary[mvary.length] = ' <table id="calendartable" width="100%" style="border:0px solid #cccccc;background-color:#ffffff" border="0" cellpadding="3" cellspacing="1">';
mvary[mvary.length] = ' <tr>';
for(var i = 0; i < 7; i++){
mvary[mvary.length] = ' <th style="font-weight:normal;background-color:' + calendar.colors["tr_bg"] + ';color:' + calendar.colors["tr_word"] + ';">' + calendar.language["weeks"][this.lang][i] + '</th>';
}
mvary[mvary.length] = ' </tr>';
for(var i = 0; i < 6;i++){
mvary[mvary.length] = ' <tr align="center">';
for(var j = 0; j < 7; j++){
if (j == 0){
mvary[mvary.length] = ' <td style="cursor:default;color:' + calendar.colors["sun_word"] + ';"></td>';
} else if(j == 6){
mvary[mvary.length] = ' <td style="cursor:default;color:' + calendar.colors["sat_word"] + ';"></td>';
} else{
mvary[mvary.length] = ' <td style="cursor:default;"></td>';
}
}
mvary[mvary.length] = ' </tr>';
}
mvary[mvary.length] = ' <tr style="background-color:' + calendar.colors["input_bg"] + ';">';
mvary[mvary.length] = ' <th colspan="2"><input name="calendarclear" type="button" id="calendarclear" value="' + calendar.language["clear"][this.lang] + '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:100%;height:20px;font-size:12px;"/></th>';
mvary[mvary.length] = ' <th colspan="3"><input name="calendartoday" type="button" id="calendartoday" value="' + calendar.language["today"][this.lang] + '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:100%;height:20px;font-size:12px;"/></th>';
mvary[mvary.length] = ' <th colspan="2"><input name="calendarclose" type="button" id="calendarclose" value="' + calendar.language["close"][this.lang] + '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:100%;height:20px;font-size:12px;"/></th>';
mvary[mvary.length] = ' </tr>';
mvary[mvary.length] = ' </table>';
mvary[mvary.length] = ' </div>';
this.panel.innerhtml = mvary.join("");

var obj = this.getelementbyid("prevmonth");
obj.onclick = function (){calendar.goprevmonth(calendar);}
obj.onblur = function (){calendar.onblur();}
this.prevmonth= obj;

obj = this.getelementbyid("nextmonth");
obj.onclick = function (){calendar.gonextmonth(calendar);}
obj.onblur = function (){calendar.onblur();}
this.nextmonth= obj;


obj = this.getelementbyid("calendarclear");
obj.onclick = function (){calendar.datecontrol.value = "";calendar.hide();}
this.calendarclear = obj;

obj = this.getelementbyid("calendarclose");
obj.onclick = function (){calendar.hide();}
this.calendarclose = obj;

obj = this.getelementbyid("calendaryear");
obj.onchange = function (){calendar.update(calendar);}
obj.onblur = function (){calendar.onblur();}
this.calendaryear = obj;

obj = this.getelementbyid("calendarmonth");
with(obj)
{
onchange = function (){calendar.update(calendar);}
onblur = function (){calendar.onblur();}
}this.calendarmonth = obj;

obj = this.getelementbyid("calendartoday");
obj.onclick = function (){
var today = new date();
calendar.date = today;
calendar.year = today.getfullyear();
calendar.month = today.getmonth();
calendar.changeselect();
calendar.binddata();
calendar.datecontrol.value = today.format(calendar.dateformatstyle);
calendar.hide();
}
this.calendartoday = obj;
}

//年份下拉框绑定数据
calendar.prototype.bindyear = function(){
var cy = this.calendaryear;
cy.length = 0;
for (var i = this.beginyear; i <= this.endyear; i++){
cy.options[cy.length] = new option(i + calendar.language["year"][this.lang], i);
}
}

//月份下拉框绑定数据
calendar.prototype.bindmonth = function(){
var cm = this.calendarmonth;
cm.length = 0;
for (var i = 0; i < 12; i++){
cm.options[cm.length] = new option(calendar.language["months"][this.lang][i], i);
}
}

//向前一月
calendar.prototype.goprevmonth = function(e){
if (this.year == this.beginyear && this.month == 0){return;}
this.month--;
if (this.month == -1){
this.year--;
this.month = 11;
}
this.date = new date(this.year, this.month, 1);
this.changeselect();
this.binddata();
}

//向后一月
calendar.prototype.gonextmonth = function(e){
if (this.year == this.endyear && this.month == 11){return;}
this.month++;
if (this.month == 12){
this.year++;
this.month = 0;
}
this.date = new date(this.year, this.month, 1);
this.changeselect();
this.binddata();
}

//改变select选中状态
calendar.prototype.changeselect = function(){
var cy = this.calendaryear;
var cm = this.calendarmonth;
for (var i= 0; i < cy.length; i++){
if (cy.options[i].value == this.date.getfullyear()){
cy[i].selected = true;
break;
}
}
for (var i= 0; i < cm.length; i++){
if (cm.options[i].value == this.date.getmonth()){
cm[i].selected = true;
break;
}
}
}

//更新年、月
calendar.prototype.update = function (e){
this.year = e.calendaryear.options[e.calendaryear.selectedindex].value;
this.month = e.calendarmonth.options[e.calendarmonth.selectedindex].value;
this.date = new date(this.year, this.month, 1);
this.changeselect();
this.binddata();
}

//绑定数据到月视图
calendar.prototype.binddata = function (){
var calendar = this;
var datearray = this.getmonthviewarray(this.date.getfullyear(), this.date.getmonth());
var tds = this.getelementbyid("calendartable").getelementsbytagname("td");
for(var i = 0; i < tds.length; i++){
tds[i].style.backgroundcolor = calendar.colors["td_bg_out"];
tds[i].onclick = function () {return;}
tds[i].onmouseover = function () {return;}
tds[i].onmouseout = function () {return;}
if (i > datearray.length - 1) break;
tds[i].innerhtml = datearray[i];
if (datearray[i] != " "){
tds[i].onclick = function () {
if (calendar.datecontrol != null){
calendar.datecontrol.value = new date(calendar.date.getfullyear(),
calendar.date.getmonth(),
this.innerhtml).format(calendar.dateformatstyle);
}
calendar.hide();
}
tds[i].onmouseover = function () {
this.style.backgroundcolor = calendar.colors["td_bg_over"];
}
tds[i].onmouseout = function () {
this.style.backgroundcolor = calendar.colors["td_bg_out"];
}
if (new date().format(calendar.dateformatstyle) ==
new date(calendar.date.getfullyear(),
calendar.date.getmonth(),
datearray[i]).format(calendar.dateformatstyle)) {
tds[i].style.backgroundcolor = calendar.colors["cur_bg"];
tds[i].onmouseover = function () {
this.style.backgroundcolor = calendar.colors["td_bg_over"];
}
tds[i].onmouseout = function () {
this.style.backgroundcolor = calendar.colors["cur_bg"];
}
}//end if

//设置已被选择的日期单元格背影色
if (calendar.datecontrol != null && calendar.datecontrol.value == new date(calendar.date.getfullyear(),
calendar.date.getmonth(),
datearray[i]).format(calendar.dateformatstyle)) {
tds[i].style.backgroundcolor = calendar.colors["sel_bg"];
tds[i].onmouseover = function () {
this.style.backgroundcolor = calendar.colors["td_bg_over"];
}
tds[i].onmouseout = function () {
this.style.backgroundcolor = calendar.colors["sel_bg"];
}
}
}
}
}

//根据年、月得到月视图数据(数组形式)
calendar.prototype.getmonthviewarray = function (y, m) {
var mvarray = [];
var dayoffirstday = new date(y, m, 1).getday();
var daysofmonth = new date(y, m + 1, 0).getdate();
for (var i = 0; i < 42; i++) {
mvarray[i] = " ";
}
for (var i = 0; i < daysofmonth; i++){
mvarray[i + dayoffirstday] = i + 1;
}
return mvarray;
}

//扩展 document.getelementbyid(id) 多浏览器兼容性 from meizz tree source
calendar.prototype.getelementbyid = function(id){
if (typeof(id) != "string" || id == "") return null;
if (document.getelementbyid) return document.getelementbyid(id);
if (document.all) return document.all(id);
try {return eval(id);} catch(e){ return null;}
}

//扩展 object.getelementsbytagname(tagname)
calendar.prototype.getelementsbytagname = function(object, tagname){
if (document.getelementsbytagname) return document.getelementsbytagname(tagname);
if (document.all) return document.all.tags(tagname);
}

//取得html控件绝对位置
calendar.prototype.getabspoint = function (e){
var x = e.offsetleft;
var y = e.offsettop;
while(e = e.offsetparent){
x += e.offsetleft;
y += e.offsettop;
}
return {"x": x, "y": y};
}

//显示日历
calendar.prototype.show = function (dateobj, popcontrol) {
if (dateobj == null){
throw new error("arguments[0] is necessary")
}
this.datecontrol = dateobj;

this.date = (dateobj.value.length > 0) ? new date(dateobj.value.todate(this.dateformatstyle)) : new date() ;//若为空则显示当前月份
this.year = this.date.getfullyear();
this.month = this.date.getmonth();
this.changeselect();
this.binddata();
if (popcontrol == null){
popcontrol = dateobj;
}
var xy = this.getabspoint(popcontrol);
this.panel.style.left = xy.x -25 + "px";
this.panel.style.top = (xy.y + dateobj.offsetheight) + "px";

this.panel.style.display = "";
this.container.style.display = "";

dateobj.onblur = function(){calendar.onblur();}
this.container.onmouseover = function(){isfocus=true;}
this.container.onmouseout = function(){isfocus=false;}
}

//隐藏日历
calendar.prototype.hide = function() {
this.panel.style.display = "none";
this.container.style.display = "none";
isfocus=false;
}

//焦点转移时隐藏日历
calendar.prototype.onblur = function() {
if(!isfocus){this.hide();}
}
document.write('<div id="containerpanel" style="display:none;"><div id="calendarpanel" style="position: absolute;display: none;z-index: 9999;');
document.write('background-color: #ffffff;border: 1px solid #cccccc;width:175px;font-size:12px;margin-left:25px;"></div>');
if(document.all)
{
document.write('<iframe style="position:absolute;z-index:2000;width:expression(this.previoussibling.offsetwidth);');
document.write('height:expression(this.previoussibling.offsetheight);');
document.write('left:expression(this.previoussibling.offsetleft);top:expression(this.previoussibling.offsettop);');
document.write('display:expression(this.previoussibling.style.display);" scrolling="no" frameborder="no"></iframe>');
}
document.write('</div>');

3.保存文件后,即完成了js日历控件的编写。

4.在相应需要日历控件的源处写在head标签下面写
复制代码 代码如下:

<script type="text/javascript" src="../js/calendar.js"></script>

其中,src是你javascript的位置

5.所有填写日期的textbox,加这两个属性:readonly="true" onclick="selectdate(this)"

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网