会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
simon
博客园
首页
新随笔
联系
订阅
管理
日期选择器作者:红心男孩 日期:2005-02-23
HTML代码
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <meta name="GENERATOR" content="Microsoft FrontPage 4.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <title>New Page 1</title> <script language=javascript> var DS_x,DS_y; function dateSelector() //构造dateSelector对象,用来实现一个日历形式的日期输入框。 { var myDate=new Date(); this.year=myDate.getFullYear(); //定义year属性,年份,默认值为当前系统年份。 this.month=myDate.getMonth()+1; //定义month属性,月份,默认值为当前系统月份。 this.date=myDate.getDate(); //定义date属性,日,默认值为当前系统的日。 this.inputName=''; //定义inputName属性,即输入框的name,默认值为空。注意:在同一页中出现多个日期输入框,不能有重复的name! this.display=display; //定义display方法,用来显示日期输入框。 } function display() //定义dateSelector的display方法,它将实现一个日历形式的日期选择框。 { var week=new Array('日','一','二','三','四','五','六'); document.write("<style type=text/css>"); document.write(" .ds_font td,span { font: normal 12px 宋体; color: #000000; }"); document.write(" .ds_border { border: 1px solid #000000; cursor: hand; background-color: #DDDDDD }"); document.write(" .ds_border2 { border: 1px solid #000000; cursor: hand; background-color: #DDDDDD }"); document.write("</style>"); var M=new String(this.month); var d=new String(this.date); if(M.length==1&&d.length==1){ document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-0"+this.month+"-0"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");} else if(M.length==1&&d.length==2){ document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-0"+this.month+"-"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");} else if(M.length==2&&d.length==1){ document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-"+this.month+"-0"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");} else if(M.length==2&&d.length==2){ document.write("<input style='text-align:center;' id='DS_"+this.inputName+"' name='"+this.inputName+"' value='"+this.year+"-"+this.month+"-"+this.date+"' title=双击可进行编缉 ondblclick='this.readOnly=false;this.focus()' onblur='this.readOnly=true' readonly>");} document.write("<button style='width:60px;height:18px;font-size:12px;margin:1px;border:1px solid #A4B3C8;background-color:#DFE7EF;' type=button onclick=this.nextSibling.style.display='block' onfocus=this.blur()>选择日期</button>"); document.write("<div style='position:absolute;display:none;text-align:center;width:0px;height:0px;overflow:visible' onselectstart='return false;'>"); document.write(" <div style='position:absolute;left:-60px;top:20px;width:142px;height:165px;background-color:#F6F6F6;border:1px solid #245B7D;' class=ds_font>"); document.write(" <table cellpadding=0 cellspacing=1 width=140 height=20 bgcolor=#CEDAE7 onmousedown='DS_x=event.x-parentNode.style.pixelLeft;DS_y=event.y-parentNode.style.pixelTop;setCapture();' onmouseup='releaseCapture();' onmousemove='dsMove(this.parentNode)' style='cursor:move;'>"); document.write(" <tr align=center>"); document.write(" <td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=subYear(this) title='减小年份'><<</td>"); document.write(" <td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=subMonth(this) title='减小月份'><</td>"); document.write(" <td width=52%><b>"+this.year+"</b><b>年</b><b>"+this.month+"</b><b>月</b></td>"); document.write(" <td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=addMonth(this) title='增加月份'>></td>"); document.write(" <td width=12% onmouseover=this.className='ds_border' onmouseout=this.className='' onclick=addYear(this) title='增加年份'>>></td>"); document.write(" </tr>"); document.write(" </table>"); document.write(" <table cellpadding=0 cellspacing=0 width=140 height=20 onmousedown='DS_x=event.x-parentNode.style.pixelLeft;DS_y=event.y-parentNode.style.pixelTop;setCapture();' onmouseup='releaseCapture();' onmousemove='dsMove(this.parentNode)' style='cursor:move;'>"); document.write(" <tr align=center>"); for(i=0;i<7;i++) document.write(" <td>"+week[i]+"</td>"); document.write(" </tr>"); document.write(" </table>"); document.write(" <table cellpadding=0 cellspacing=2 width=140 bgcolor=#EEEEEE>"); for(i=0;i<6;i++) { document.write(" <tr align=center>"); for(j=0;j<7;j++) document.write(" <td width=10% height=16 onmouseover=if(this.innerText!=''&&this.className!='ds_border2')this.className='ds_border' onmouseout=if(this.className!='ds_border2')this.className='' onclick=getValue(this,document.all('DS_"+this.inputName+"'))></td>"); document.write(" </tr>"); } document.write(" </table>"); document.write(" <span style=cursor:hand onclick=this.parentNode.parentNode.style.display='none'>【关闭】</span>"); document.write(" </div>"); document.write("</div>"); dateShow(document.all("DS_"+this.inputName).nextSibling.nextSibling.childNodes[0].childNodes[2],this.year,this.month) } function subYear(obj) //减小年份 { var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes; myObj[0].innerHTML=eval(myObj[0].innerHTML)-1; dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML)) } function addYear(obj) //增加年份 { var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes; myObj[0].innerHTML=eval(myObj[0].innerHTML)+1; dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML)) } function subMonth(obj) //减小月份 { var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes; var month=eval(myObj[2].innerHTML)-1; if(month==0) { month=12; subYear(obj); } myObj[2].innerHTML=month; dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML)) } function addMonth(obj) //增加月份 { var myObj=obj.parentNode.parentNode.parentNode.cells[2].childNodes; var month=eval(myObj[2].innerHTML)+1; if(month==13) { month=1; addYear(obj); } myObj[2].innerHTML=month; dateShow(obj.parentNode.parentNode.parentNode.nextSibling.nextSibling,eval(myObj[0].innerHTML),eval(myObj[2].innerHTML)) } function dateShow(obj,year,month) //显示各月份的日 { var myDate=new Date(year,month-1,1); var today=new Date(); var day=myDate.getDay(); var selectDate=obj.parentNode.parentNode.previousSibling.previousSibling.value.split('-'); var length; switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: length=31; break; case 4: case 6: case 9: case 11: length=30; break; case 2: if((year%4==0)&&(year%100!=0)||(year%400==0)) length=29; else length=28; } for(i=0;i<obj.cells.length;i++) { obj.cells[i].innerHTML=''; obj.cells[i].style.color=''; obj.cells[i].className=''; } for(i=0;i<length;i++) { obj.cells[i+day].innerHTML=(i+1); if(year==today.getFullYear()&&(month-1)==today.getMonth()&&(i+1)==today.getDate()) obj.cells[i+day].style.color='red'; if(year==eval(selectDate[0])&&month==eval(selectDate[1])&&(i+1)==eval(selectDate[2])) obj.cells[i+day].className='ds_border2'; } } function getValue(obj,inputObj) //把选择的日期传给输入框 { var myObj=inputObj.nextSibling.nextSibling.childNodes[0].childNodes[0].cells[2].childNodes; if(obj.innerHTML) if(obj.innerHTML.length==1&&myObj[2].innerHTML.length==1) inputObj.value=myObj[0].innerHTML+"-0"+myObj[2].innerHTML+"-0"+obj.innerHTML; else if(obj.innerHTML.length==1&&myObj[2].innerHTML.length==2) inputObj.value=myObj[0].innerHTML+"-"+myObj[2].innerHTML+"-0"+obj.innerHTML; else if(obj.innerHTML.length==2&&myObj[2].innerHTML.length==1) inputObj.value=myObj[0].innerHTML+"-0"+myObj[2].innerHTML+"-"+obj.innerHTML; else if(obj.innerHTML.length==2&&myObj[2].innerHTML.length==2) inputObj.value=myObj[0].innerHTML+"-"+myObj[2].innerHTML+"-"+obj.innerHTML; inputObj.nextSibling.nextSibling.style.display='none'; for(i=0;i<obj.parentNode.parentNode.parentNode.cells.length;i++) obj.parentNode.parentNode.parentNode.cells[i].className=''; obj.className='ds_border2' } function dsMove(obj) //实现层的拖移 { if(event.button==1) { var X=obj.clientLeft; var Y=obj.clientTop; obj.style.pixelLeft=X+(event.x-DS_x); obj.style.pixelTop=Y+(event.y-DS_y); } } </script> </head> <body> 从 <script language=javascript> var myDate=new dateSelector(); myDate.year--; myDate.inputName='start_date'; //注意这里设置输入框的name,同一页中日期输入框,不能出现重复的name。 myDate.display(); </script> 到 <script language=javascript> myDate.year++; myDate.inputName='end_date'; //注意这里设置输入框的name,同一页中的日期输入框,不能出现重复的name。 myDate.display(); </script> </body> </html>
HTML代码
<script> //定义显示时间的格式 //1. 年-月-日 如:2002-3-6 //2. 月/日/年 如:3/6/2002 var showType=1;//默认为1;2002-3-6 //中文月份,如果想显示英文月份,修改下面的注释 /*var months = new Array("January?, "February?, "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");*/ var months = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"); var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); //中文周 如果想显示 英文的,修改下面的注释 /*var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");*/ var days = new Array("日","一", "二", "三", "四", "五", "六"); var getObject,oldObject;//取得当前对象 function getDays(month, year) { //下面的这段代码是判断当前是否是闰年的 if (1 == month) return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28; else return daysInMonth[month]; } function getToday() { //得到今天的年,月,日 this.now = new Date(); this.year = this.now.getFullYear(); this.month = this.now.getMonth(); this.day = this.now.getDate(); } today = new getToday(); function newCalendar() { today = new getToday(); var parseYear = parseInt(document.all.year [document.all.year.selectedIndex].text); var newCal = new Date(parseYear, document.all.month.selectedIndex, 1); var day = -1; var startDay = newCal.getDay(); var daily = 0; if ((today.year == newCal.getFullYear()) &&(today.month == newCal.getMonth())) day = today.day; var tableCal = document.all.calendar.tBodies.dayList; var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear()); for (var intWeek = 0; intWeek < tableCal.rows.length;intWeek++) for (var intDay = 0;intDay < tableCal.rows[intWeek].cells.length;intDay++) { var cell = tableCal.rows[intWeek].cells[intDay]; if ((intDay == startDay) && (0 == daily)) daily = 1; if(day==daily) //今天,调用今天的Class cell.className = "today"; else if(intDay==6) //周六 cell.className = "sunday"; else if (intDay==0) //周日 cell.className ="satday"; else //平常 cell.className="normal"; if ((daily > 0) && (daily <= intDaysInMonth)) { cell.innerText = daily; daily++; } else cell.innerText = ""; } } function getDate() { var sDate; //这段代码处理鼠标点击的情况 if ("TD" == event.srcElement.tagName) if ("" != event.srcElement.innerText) {//从这儿可以改显示日期的格式 if(showType==1) sDate = document.all.year.value + "-" + document.all.month.value + "-" + event.srcElement.innerText + ""; else if(showType==2) sDate = document.all.month.value + "/" + event.srcElement.innerText +"/"+ document.all.year.value; else sDate = document.all.year.value + "-" + document.all.month.value + "-" + event.srcElement.innerText + ""; getObject.value=sDate; // setUpdateFlag(getObject);//触发事件,保存数据 HideLayer(); } } function HideLayer() { Layer.style.visibility = "hidden"; } function LayerShow(){ Layer.style.visibility = "visible"; } function ShowLayer(t,l) { if(oldObject==null)oldObject=getObject;//付值 if(Layer.style.visibility != "visible" && oldObject==getObject){//判断是否操作同一表格 //if(l>480)l=l-131; Layer.style.top = t+document.body.scrollTop; Layer.style.left = l+document.body.scrollLeft; Layer.style.visibility = "visible"; oldObject=getObject; } else if(oldObject==getObject){ HideLayer(); } else{ //if(l>480)l=l-131; Layer.style.top = t+document.body.scrollTop; Layer.style.left = l+document.body.scrollLeft; Layer.style.visibility = "visible"; oldObject=getObject; } } function selectDate(x){ window.event.cancelBubble = true;//冒泡,使触发页面事件失效 getObject=x; var xx=event.clientX;//取得x坐标; var yy=event.clientY;//取得y坐标; newCalendar(); ShowLayer(yy,xx);//显示出div } function show(){//冒泡,点击层失效 window.event.cancelBubble = true; } //定义移动 var Obj='' document.onmouseup=MUp document.onmousemove=MMove function MDown(Object){ Obj=Object.id document.all(Obj).setCapture(); pX=event.x-document.all(Obj).style.pixelLeft; pY=event.y-document.all(Obj).style.pixelTop; } function MMove(){ if(Obj!=''){ document.all(Obj).style.left=event.x-pX; document.all(Obj).style.top=event.y-pY; } } function MUp(){ if(Obj!=''){ document.all(Obj).releaseCapture(); Obj=''; } } //定义div显示日期; function getLayer(){ document.write("<div id=\"Layer\" onclick=show() style=\"position:absolute; width:131; z-index:2; border:1px inset black; background-color: #f0f8ff; layer-background-color: #f0f8ff; visibility: hidden; height: 38; left:58; top:137;\"><input type=\"hidden\" name=\"ret\"><table bgcolor=#99B6D0 width=100% style=\"cursor:move\" onmousedown=MDown(Layer)><tr align=center><td>按住鼠标拖动</td></tr></table><table id=\"calendar\" cellpadding=\"0\" align=\"center\"><thead><tr><td colspan=7 align=CENTER><select id=\"month\" onChange=\"newCalendar()\" name=\"select\" class=\"smallSel\">"); for (var intLoop = 0; intLoop < months.length; intLoop++) document.write("<OPTION VALUE= " + (intLoop + 1) + " " + (today.month == intLoop ? "Selected" : "") + ">" + months[intLoop]); document.write("</select><select id=\"year\" onChange=\"newCalendar()\" name=\"select\" class=\"smallSel\">"); for (var intLoop = today.year-50; intLoop < (today.year + 5); intLoop++) document.write("<OPTION VALUE= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop); document.write("</select></td></tr><tr class=\"days\">"); document.write("<TD class=satday>" + days[0] + "</TD>"); for (var intLoop = 1; intLoop < days.length-1;intLoop++) document.write("<TD>" + days[intLoop] + "</TD>"); document.write("<TD class=sunday>" + days[intLoop] + "</TD>"); document.write(" </tr> </thead> <tbody border=1 cellspacing=\"0\" cellpadding=\"0\" id=\"dayList\" align=CENTER ONCLICK='getDate()'>"); for (var intWeeks = 0; intWeeks < 6; intWeeks++) { document.write("<TR style='cursor:hand'>"); for (var intDays = 0; intDays < days.length;intDays++) document.write("<TD></TD>"); document.write("</TR>"); } document.write("</tbody></table></div>"); document.body.onclick=HideLayer;//触发点击页面事件,关闭窗口 document.oncontextmenu=HideLayer;//触发点击页面右键,关闭窗口 } //显示css document.write("<style>TABLE {font-family:宋体,MS SONG,SimSun,tahoma,sans-serif; font-size:9pt;border:0px}.drpdwn {font-family:宋体,MS SONG,SimSun,tahoma,sans-serif;font-size:9pt;color:#000066;background-color:#FFFFFF} Select.smallSel{ BACKGROUND-COLOR: #ffffff; COLOR: #000080; FONT-SIZE: 9pt} .normal{BACKGROUND: #ffffff} .today {font-weight:bold;BACKGROUND: #6699cc} .satday{color:green} .sunday{color:red} .days {font-weight:bold} .Arraw {color:#0000BB; cursor:hand; font-family:Webdings; font-size:9pt}</style>"); getLayer();//显示div </script> <input type=text size=10 name=aa style=border-top:0;border-left:0;border-right:0;border-color=red readonly><a href=# onclick=selectDate(document.all.aa)>请选择</a>
http://www.kekecn.com/blog/article.asp?id=362
posted on
2007-02-09 17:21
mbskys
阅读(
196
) 评论(
0
)
收藏
举报
刷新页面
返回顶部