JavaScript实现popup日期控件(一)

<script type="text/javascript"> function GetDefineX(ObjectID) { var iPositionX=ObjectID.offsetLeft; while(ObjectID=ObjectID.offsetParent) { iPositionX+=ObjectID.offsetLeft-ObjectID.scrollLeft; } return iPositionX; } function GetDefineY(ObjectID) { var iPositionY=ObjectID.offsetTop; while(ObjectID=ObjectID.offsetParent) { iPositionY+=ObjectID.offsetTop-ObjectID.scrollTop; } return iPositionY; } String.prototype.Trim = function() { return this.replace(/(^/s*)|(/s*$)/g, ""); } String.prototype.LTrim = function() { return this.replace(/(^/s*)/g, ""); } String.prototype.RTrim = function() { return this.replace(/(/s*$)/g, ""); } var BSPopup = window.createPopup(); BSDate.cl_resClickLater = '请在页面显示完了之后,再点击。'; BSDate.cl_resDateDrop = '▼'; BSDate.cl_resDateTitle = '选择日期'; BSDate.cl_resDateUp = '▲'; BSDate.cl_resDay = '日'; BSDate.cl_resMonth = '月'; BSDate.cl_resMonth1 = '一月'; BSDate.cl_resMonth10 = '十月'; BSDate.cl_resMonth11 = '十一月'; BSDate.cl_resMonth12 = '十二月'; BSDate.cl_resMonth2 = '二月'; BSDate.cl_resMonth3 = '三月'; BSDate.cl_resMonth4 = '四月'; BSDate.cl_resMonth5 = '五月'; BSDate.cl_resMonth6 = '六月'; BSDate.cl_resMonth7 = '七月'; BSDate.cl_resMonth8 = '八月'; BSDate.cl_resMonth9 = '九月'; BSDate.cl_resMonthNext = '下月'; BSDate.cl_resMonthNextBtn = '》'; BSDate.cl_resMonthPrev = '上月'; BSDate.cl_resMonthPrevBtn = '《'; BSDate.cl_resWeek1 = '一'; BSDate.cl_resWeek2 = '二'; BSDate.cl_resWeek3 = '三'; BSDate.cl_resWeek4 = '四'; BSDate.cl_resWeek5 = '五'; BSDate.cl_resWeek6 = '六'; BSDate.cl_resWeek7 = '日'; BSDate.cl_resYear = '年'; BSDate.dateHTML = ""; BSDate.m_iDatePickerCount=0; BSDate.cl_dpMaxYear=2150; BSDate.cl_dpMaxMonth=11; BSDate.cl_dpMaxDay=31; BSDate.cl_dpMinYear=1901; BSDate.cl_dpMinMonth=0; BSDate.cl_dpMinDay=1; BSPopup_style_title = "FONT-WEIGHT: normal;FONT-SIZE: 9pt;COLOR: #fffacd;background-COLOR: #00008b;"; BSPopup_style_frameinput = "height:18px;width:15px;BORDER: black 0px solid;FONT-WEIGHT: normal;FONT-SIZE: 9pt;black: blue;LINE-HEIGHT: normal;FONT-STYLE: normal;background-COLOR: transparent;Text-ALIGN: right;FONT-VARIANT: normal;"; BSPopup_style_framebtn = "BORDER-RIGHT: black 1px solid;border-TOP: white 1px solid;FONT-WEIGHT: normal;FONT-SIZE: 9pt;border-LEFT: white 1px solid;width: 16px;COLOR: blue;LINE-HEIGHT: normal;border-BOTTOM: black 1px solid;FONT-STYLE: normal;background-COLOR: silver;FONT-VARIANT: normal;"; BSPopup_style_framebtnOK = "BORDER-RIGHT: black 1px solid;border-TOP: white 1px solid;FONT-WEIGHT: normal;FONT-SIZE: 9pt;border-LEFT: white 1px solid;width: 30px;COLOR: blue;LINE-HEIGHT: normal;border-BOTTOM: black 1px solid;FONT-STYLE: normal;background-COLOR: silver;FONT-VARIANT: normal;"; BSPopup_style_framebtnOK2 = "background-COLOR: silver;BORDER-RIGHT: black 1px solid;border-TOP: white 1px solid;width: 16px;border-LEFT: white 1px solid;border-BOTTOM: black 1px solid;height: 8px;font-family:Webdings;font-size: 7px;line-height: 2px;padding-left: 2px;cursor: default;"; BSPopup_style_weekname = "FONT-WEIGHT: normal;COLOR: #00008b;LINE-HEIGHT: normal;BORDER-BOTTOM: black 1px solid;FONT-SIZE: 9pt;FONT-STYLE: normal;TEXT-ALIGN: center;FONT-VARIANT: normal;"; BSPopup_style_table = "BORDER-TOP: lightgrey 1px solid;BORDER-LEFT: lightgrey 1px solid;CURSOR: default;BORDER-right: black 1px solid;BORDER-BOTTOM: black 1px solid;POSITION: absolute;BACKGROUND-COLOR: #ffffff;"; BSPopup_style_DPCellOther = "FONT-WEIGHT: normal;FONT-SIZE: 9pt;CURSOR: hand;COLOR: silver;LINE-HEIGHT: normal;FONT-STYLE: normal;TEXT-ALIGN: center;FONT-VARIANT: normal;"; BSPopup_style_DPCell = "FONT-WEIGHT: normal;FONT-SIZE: 9pt;CURSOR: hand;COLOR: #00008b;LINE-HEIGHT: normal;FONT-STYLE: normal;TEXT-ALIGN: center;FONT-VARIANT: normal;"; BSPopup_style_DPCellSelect = "FONT-WEIGHT: normal;FONT-SIZE: 9pt;CURSOR: hand;COLOR: #000000;LINE-HEIGHT: normal;FONT-STYLE: normal;TEXT-ALIGN: center;FONT-VARIANT: normal;BACKGROUND-COLOR: #ffa07a;"; function KeyFilter(type) { var berr=false; switch(type) { case 'date': if (!(event.keyCode == 45 || event.keyCode == 47 || (event.keyCode>=48 && event.keyCode<=57))) berr=true; break; case 'number': if (!(event.keyCode>=48 && event.keyCode<=57)) berr=true; break; case 'cy': if (!(event.keyCode == 46 || (event.keyCode>=48 && event.keyCode<=57))) berr=true; break; case 'long': if (!(event.keyCode == 45 || (event.keyCode>=48 && event.keyCode<=57))) berr=true; break; case 'double': if (!(event.keyCode == 45 || event.keyCode == 46 || (event.keyCode>=48 && event.keyCode<=57))) berr=true; break; default: if (event.keyCode == 35 || event.keyCode == 37 || event.keyCode==38) berr=true; } return !berr; } function dateCompare(thisDt, dtCompare){ var hr = -1; if(thisDt && dtCompare) { if(thisDt.getFullYear()>dtCompare.getFullYear()) hr=1; else if(thisDt.getFullYear() dtCompare.getMonth()) hr=1; else if(thisDt.getMonth() dtCompare.getDate()) hr=1; else if(thisDt.getDate() 2) { y=y.substring(y.length-2,y.length); } sFormat=sFormat.replace(/yy/ig,y); var temp = dt.getMonth()+1; if (temp < 10){ temp = "0"+temp; } sFormat=sFormat.replace(/mm/ig,temp); temp = dt.getDate(); if (temp < 10){ temp = "0"+temp; } sFormat=sFormat.replace(/dd/ig,temp); temp = dt.getHours(); if (temp < 10){ temp = "0"+temp; } sFormat=sFormat.replace(/hh24/ig,temp); temp = dt.getMinutes(); if (temp < 10){ temp = "0"+temp; } sFormat=sFormat.replace(/mi/ig,temp); sFormat=sFormat.replace(/ss/ig,"00"); return sFormat; } function StringToDate(inStr){ var dt = new Date(); if (inStr != null && inStr != ""){ var dList = null; var tList = null; if (inStr.indexOf(' ') < 1){ dList = inStr.split('-'); } else{ dList = (inStr.substring(0, inStr.indexOf(' '))).split('-'); tList = (inStr.substring(inStr.indexOf(' ')+1, inStr.length)).split(':'); } dt.setYear(dList[0]); dt.setMonth(dList[1]-1); dt.setDate(dList[2]); if (tList != null){ dt.setHours(tList[0]); dt.setMinutes(tList[1]); } else{ dt.setHours(0); dt.setMinutes(0); } } return dt; } function BSDate(name, elmName, readonly, showTime){ this.name = name || "bsDate"; this.readonly = readonly || false; this.elmName = elmName || "bsDateObj"; this.showTime; if (showTime == null){ this.showTime = true; } else{ this.showTime = showTime; } this.bsdate = new Date(); this.getMonthName = function(lMonth){ var mnArr=new Array(BSDate.cl_resMonth1,BSDate.cl_resMonth2,BSDate.cl_resMonth3,BSDate.cl_resMonth4,BSDate.cl_resMonth5,BSDate.cl_resMonth6, BSDate.cl_resMonth7,BSDate.cl_resMonth8,BSDate.cl_resMonth9,BSDate.cl_resMonth10,BSDate.cl_resMonth11,BSDate.cl_resMonth12); return mnArr[lMonth]; } this.getDateObj = function (){ return this.bsdate; } this.getDateStr = function (){ return document.getElementById(this.elmName).value; } this.updateDateTime = function(dt, lYear, lMonth, lDay, lHours, lMins){ if (this.readonly){ return; } if (dt == null){ lYear=parseInt(lYear,10); lMonth=parseInt(lMonth,10); lDay=parseInt(lDay,10); lHours=parseInt(lHours,10); lMins=parseInt(lMins,10); if(!(isNaN(lYear) || isNaN(lMonth) || isNaN(lDay) || isNaN(lHours) || isNaN(lMins))){ if (lYear<=BSDate.cl_dpMinYear){ lYear = BSDate.cl_dpMinYear; } else if (lYear>=BSDate.cl_dpMaxYear){ lYear = BSDate.cl_dpMaxYear; } this.bsdate.setYear(lYear); this.bsdate.setMonth(lMonth-1); this.bsdate.setDate(lDay); this.bsdate.setHours(lHours); this.bsdate.setMinutes(lMins); } } else{ this.bsdate = dt; } this.setInputDate(); } this.updateDate = function(lYear, lMonth, lDay){ if (this.readonly){ return; } lYear=parseInt(lYear,10); lMonth=parseInt(lMonth,10); lDay=parseInt(lDay,10); if(!(isNaN(lYear) || isNaN(lMonth) || isNaN(lDay))){ if (lYear<=BSDate.cl_dpMinYear){ lYear = BSDate.cl_dpMinYear; } else if (lYear>=BSDate.cl_dpMaxYear){ lYear = BSDate.cl_dpMaxYear; } this.bsdate.setYear(lYear); this.bsdate.setMonth(lMonth-1); this.bsdate.setDate(lDay); } this.setInputDate(); } this.updateTime = function(lHours, lMins){ if (this.readonly){ return; } lHours=parseInt(lHours,10); lMins=parseInt(lMins,10); if(!(isNaN(lHours) || isNaN(lMins))){ this.bsdate.setHours(lHours); this.bsdate.setMinutes(lMins); } this.setInputDate(); } this.setReadonly = function(readonly){ this.readonly = readonly || false; if (document.getElementById(this.elmName) != null){ var col = "black"; if (this.readonly){ col = "silver"; } document.getElementById(this.name+"_input_year").readOnly = this.readonly; document.getElementById(this.name+"_input_month").readOnly = this.readonly; document.getElementById(this.name+"_input_day").readOnly = this.readonly; document.getElementById(this.name+"_input_hour").readOnly = this.readonly; document.getElementById(this.name+"_input_min").readOnly = this.readonly; document.getElementById(this.name+"_btn").style.color = col; } } this.setTimeShow = function(showTime){ this.showTime = showTime || false; if (document.getElementById(this.elmName) != null){ var col = "none"; if (this.showTime){ col = ""; } var stformt="yyyy-mm-dd hh24:mi:ss"; if (!this.showTime){ stformt = "yyyy-mm-dd"; } document.getElementById(this.name+"_input_hour").style.display = col; document.getElementById(this.name+"_input_mh").style.display = col; document.getElementById(this.name+"_input_min").style.display = col; document.getElementById(this.elmName).value = dateFormat(this.bsdate, stformt); } } this.setDateHTML = function(){ this.setInputDate(); var dt = new Date(this.bsdate.getFullYear(),this.bsdate.getMonth(),1); var tempDt=new Date(this.bsdate.getFullYear(),this.bsdate.getMonth(),1-dt.getDay()); BSDate.dateHTML = "
"; BSDate.dateHTML += "
"; BSDate.dateHTML += "
"+BSDate.cl_resMonthPrevBtn+"
"; BSDate.dateHTML += "
"+this.getMonthName(this.bsdate.getMonth())+" "+this.bsdate.getFullYear()+"
"; BSDate.dateHTML += "
"+BSDate.cl_resMonthNextBtn+"
"; BSDate.dateHTML += " "; BSDate.dateHTML += "
"; BSDate.dateHTML += "
 "+BSDate.cl_resWeek7+" 
"; BSDate.dateHTML += "
 "+BSDate.cl_resWeek1+" 
"; BSDate.dateHTML += "
 "+BSDate.cl_resWeek2+" 
"; BSDate.dateHTML += "
 "+BSDate.cl_resWeek3+" 
"; BSDate.dateHTML += "
 "+BSDate.cl_resWeek4+" 
"; BSDate.dateHTML += "
 "+BSDate.cl_resWeek5+" 
"; BSDate.dateHTML += "
 "+BSDate.cl_resWeek6+" 
"; BSDate.dateHTML += " "; var day=tempDt.getDate(); for(var i=2; i<8; i++){ BSDate.dateHTML += "
"; for(var j=0; j<7; j++){ BSDate.dateHTML += "
"; tempDt.setDate(day+1); day=tempDt.getDate(); } BSDate.dateHTML += "
"; } BSDate.dateHTML += "
"; BSDate.dateHTML += "
"; BSDate.dateHTML += "
"; if (this.showTime){ BSDate.dateHTML += "
"+this.bsdate.getHours()+"
"; BSDate.dateHTML += "
"; BSDate.dateHTML += "
"; BSDate.dateHTML += ""; BSDate.dateHTML += "
"+this.bsdate.getMinutes()+"
"; BSDate.dateHTML += "
"; BSDate.dateHTML += "
"; BSDate.dateHTML += ""; BSDate.dateHTML += "
"; } else{ BSDate.dateHTML += "
 
"; } BSDate.dateHTML += "
"; BSDate.dateHTML += "
"; BSDate.dateHTML += " "; BSDate.dateHTML += " "; BSDate.dateHTML += " "; BSDate.dateHTML += " "; } this.show = function(type){ var lYear=this.bsdate.getFullYear(); var lMonth=this.bsdate.getMonth()+1; if (lMonth < 10){ lMonth = "0"+lMonth; } var lDay=this.bsdate.getDate(); if (lDay < 10){ lDay = "0"+lDay; } var lHours=this.bsdate.getHours(); if (lHours < 10){ lHours = "0"+lHours; } var lMins=this.bsdate.getMinutes(); if (lMins < 10){ lMins = "0"+lMins; } var ro = "", rosty=""; if (this.readonly){ ro = " readonly=/"readonly/""; rosty="color:silver;"; } var st = "",stformt="yyyy-mm-dd hh24:mi:ss"; if (!this.showTime){ st = "display:none;"; stformt = "yyyy-mm-dd"; } var tempHTML = " "; tempHTML += " "; tempHTML += " "; tempHTML += " "+BSDate.cl_resYear+""; tempHTML += " "; tempHTML += " "+BSDate.cl_resMonth+""; tempHTML += " "; tempHTML += " "+BSDate.cl_resDay+""; tempHTML += " "; tempHTML += " :"; tempHTML += " "; tempHTML += " "+BSDate.cl_resDateDrop+""; if(typeof(this.name)=="string" && this.name.length>0){ tempHTML += " "; } tempHTML += " "; tempHTML += ""; if (type == null || type){ document.write(tempHTML); } return tempHTML; } this.showDig = function(inObjID){ if (this.readonly){ return; } var left = 128; this.setDateHTML(); if (!this.showTime){ left = 90; } BSPopup.document.body.innerHTML=BSDate.dateHTML; BSPopup.show(GetDefineX(inObjID)-left, GetDefineY(inObjID)+14, 184, 162, document.body); } this.setInputDate = function(){ if (document.getElementById(this.elmName) != null){ var lYear=this.bsdate.getFullYear(); var lMonth=this.bsdate.getMonth()+1; if (lMonth < 10){ lMonth = "0"+lMonth; } var lDay=this.bsdate.getDate(); if (lDay < 10){ lDay = "0"+lDay; } var lHours=this.bsdate.getHours(); if (lHours < 10){ lHours = "0"+lHours; } var lMins=this.bsdate.getMinutes(); if (lMins < 10){ lMins = "0"+lMins; } var stformt="yyyy-mm-dd hh24:mi:ss"; if (!this.showTime){ stformt = "yyyy-mm-dd"; } document.getElementById(this.name+"_input_year").value = lYear; document.getElementById(this.name+"_input_month").value = lMonth; document.getElementById(this.name+"_input_day").value = lDay; document.getElementById(this.name+"_input_hour").value = lHours; document.getElementById(this.name+"_input_min").value = lMins; document.getElementById(this.elmName).value = dateFormat(this.bsdate, stformt); } } this.dateBlur = function(srcType){ if (this.readonly){ return true; } var src=event.srcElement; var lYear=this.bsdate.getFullYear(); var lMonth=this.bsdate.getMonth()+1; var lDay=this.bsdate.getDate(); var lHours=this.bsdate.getHours(); var lMins=this.bsdate.getMinutes(); var val=parseInt(src.value,10); if(isNaN(val)){ val=-1; } switch(srcType) { case 'year': lYear=val==-1?lYear:val; break; case 'month': lMonth=val==-1?lMonth:val; break; case 'day': lDay=val==-1?lDay:val; break; case 'hour': lHours=val==-1?lHours:val; break; case 'min': lMins=val==-1?lMins:val; break; default:break; } this.setCurDate(lYear, lMonth, lDay, lHours, lMins); return true; } this.dateFocus = function (srcType){ if (this.readonly){ return true; } var src=event.srcElement; if(src && src.tagName=="INPUT") { switch(srcType) { case 'year': break; case 'month': break; case 'day': break; case 'hour': break; case 'min': break; default:; } src.select(); } return true; } this.keyDown = function (srcType){ if (this.readonly){ return true; } var bRefresh=true; var src=event.srcElement; var lYear=this.bsdate.getFullYear(); var lMonth=this.bsdate.getMonth(); var lDay=this.bsdate.getDate(); var lHours=this.bsdate.getHours(); var lMins=this.bsdate.getMinutes(); var lStep=0; switch(event.keyCode){ case 38: lStep=1; break; case 40: lStep=-1; break; case 13: event.keyCode=9; bRefresh=false; break; default:bRefresh=false;break; } switch(srcType) { case 'year': lYear+=lStep; break; case 'month': lMonth+=lStep; break; case 'day': lDay+=lStep; break; case 'hour': lHours+=lStep; break; case 'min': lMins+=lStep; break; default:break; } if(bRefresh){ this.setCurDate(lYear, lMonth+1, lDay, lHours, lMins); } return true; } this.setCurDate = function(lYear, lMonth, lDay, lHours, lMins){ var hr=false; lYear=parseInt(lYear,10); lMonth=parseInt(lMonth,10); lDay=parseInt(lDay,10); lHours=parseInt(lHours,10); lMins=parseInt(lMins,10); if(!(isNaN(lYear) || isNaN(lMonth) || isNaN(lDay) || isNaN(lHours) || isNaN(lHours))){ var dt=new Date(lYear, lMonth-1 , lDay, lHours, lMins); var dMax=new Date(BSDate.cl_dpMaxYear,BSDate.cl_dpMaxMonth,BSDate.cl_dpMaxDay,23,59); var dMin=new Date(BSDate.cl_dpMinYear,BSDate.cl_dpMinMonth,BSDate.cl_dpMinDay,0,0); if(dateCompare(dt, dMax)<=0 && dateCompare(dt, dMin)>=0){ this.bsdate=dt; } else if (dateCompare(dt, dMax)>0){ this.bsdate = dMax } else if (dateCompare(dt, dMin)<0){ this.bsdate = dMin } hr=true; } if(hr){ this.setInputDate(); } return hr; } this.toDay = function(event){ event.returnValue=false; var today = new Date(); this.bsdate.setYear(today.getFullYear()); this.bsdate.setMonth(today.getMonth()); this.bsdate.setDate(today.getDate()); this.bsdate.setHours(today.getHours()); this.bsdate.setMinutes(today.getMinutes()); this.setDateHTML(); BSPopup.document.body.innerHTML=BSDate.dateHTML; return false; } this.changeDay = function(event, inYear, inMonth, inDay){ this.bsdate.setYear(inYear); this.bsdate.setMonth(inMonth); this.bsdate.setDate(inDay); this.setDateHTML(); BSPopup.document.body.innerHTML=BSDate.dateHTML; return false; } this.changeHour = function(event, inHour){ event.returnValue=false; this.bsdate.setHours(this.bsdate.getHours() + inHour); this.setDateHTML(); BSPopup.document.body.innerHTML=BSDate.dateHTML; return false; } this.changeMin = function(event, inMin){ event.returnValue=false; this.bsdate.setMinutes(this.bsdate.getMinutes() + inMin); this.setDateHTML(); BSPopup.document.body.innerHTML=BSDate.dateHTML; return false; } this.changeMonth = function(event, arrFlg){ var dMax=new Date(BSDate.cl_dpMaxYear,BSDate.cl_dpMaxMonth-1,BSDate.cl_dpMaxDay,0,0); var dMin=new Date(BSDate.cl_dpMinYear,BSDate.cl_dpMinMonth+1,BSDate.cl_dpMinDay,0,0); if (arrFlg){ if (dateCompare(this.bsdate, dMin)>0){ this.bsdate.setMonth(this.bsdate.getMonth()-1); } } else{ if (dateCompare(this.bsdate, dMax)<0){ this.bsdate.setMonth(this.bsdate.getMonth()+1); } } this.setDateHTML(); BSPopup.document.body.innerHTML=BSDate.dateHTML; } } </script>
 
<script language="JavaScript"> var DateSam = new BSDate("DateSam", "elm_DateSam"); DateSam.updateTime(00, 00); DateSam.show(); </script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mynickel2000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值