永发信息网

用Javascript做一个日历控件!能写备忘

答案:2  悬赏:0  手机版
解决时间 2021-11-28 04:32
用Javascript做一个日历控件!能写备忘
最佳答案
1document.write("");
2document.write("");
3function writeIframe(){
4 var strIframe = " 66 with(WebCalendar.iframe)
67 {
68 document.writeln(strIframe); document.close();
69 for(var i=0; i<39; i++)
70 {
71 WebCalendar.dayObj[i] = eval("meizzDay"+ i);
72 WebCalendar.dayObj[i].onmouseover = dayMouseOver;
73 WebCalendar.dayObj[i].onmouseout = dayMouseOut;
74 WebCalendar.dayObj[i].onclick = returnDate;
75 }
76 }
77}
78function WebCalendar() //初始化日历的设置
79{
80 this.regInfo = "WEB Calendar ver 3.0关闭的快捷键:[Esc]";
81 this.daysMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
82 this.day = new Array(39); //定义日历展示用的数组
83 this.dayObj = new Array(39); //定义日期展示控件数组
84 this.dateStyle = null; //保存格式化后日期数组
85 this.objExport = null; //日历回传的显示控件
86 this.eventSrc = null; //日历显示的触发控件
87 this.inputDate = null; //转化外的输入的日期(d/m/yyyy)
88 this.thisYear = new Date().getFullYear(); //定义年的变量的初始值
89 this.thisMonth = new Date().getMonth()+ 1; //定义月的变量的初始值
90 this.thisDay = new Date().getDate(); //定义日的变量的初始值
91 this.today = this.thisDay +"/"+ this.thisMonth +"/"+ this.thisYear; //今天(d/m/yyyy)
92 this.iframe = window.frames("meizzCalendarIframe"); //日历的 iframe 载体
93 this.calendar = getObjectById("meizzCalendarLayer"); //日历的层
94 this.dateReg = ""; //日历格式验证的正则式
95 this.yearFall = 50; //定义年下拉框的年差值
96 this.format = "yyyy-mm-dd"; //回传日期的格式
97 this.timeShow = false; //是否返回时间
98 this.drag = true; //是否允许拖动
99 this.darkColor = "#408080"; //控件的暗色
100 this.lightColor = "#FFFFFF"; //控件的亮色
101 this.btnBgColor = "#ededed"; //控件的按钮背景色
102 this.wordColor = "#000000"; //控件的文字颜色
103 this.wordDark = "#DCDCDC"; //控件的暗文字颜色
104 this.dayBgColor = "#F5F5FA"; //日期数字背景色
105 this.todayColor = "#ff3300"; //今天在日历上的标示背景色
106 this.DarkBorder = "#D4D0C8"; //日期显示的立体表达色
107} var WebCalendar = new WebCalendar();
108function calendar() //主调函数
109{
110 var e = window.event.srcElement;
111 writeIframe();
112 var o = WebCalendar.calendar.style; WebCalendar.eventSrc = e;
113 if (arguments.length == 0) WebCalendar.objExport = e;
114 else WebCalendar.objExport = eval(arguments[0]);
115 WebCalendar.iframe.tableWeek.style.cursor = WebCalendar.drag ? "move" : "default";
116 var t = e.offsetTop, h = e.clientHeight, l = e.offsetLeft, p = e.type;
117 while (e = e.offsetParent){t += e.offsetTop; l += e.offsetLeft;}
118 o.display = ""; WebCalendar.iframe.document.body.focus();
119 var cw = WebCalendar.calendar.clientWidth, ch = WebCalendar.calendar.clientHeight;
120 var dw = document.body.clientWidth, dl = document.body.scrollLeft, dt = document.body.scrollTop;
121
122 if (document.body.clientHeight + dt - t - h >= ch) o.top = (p=="image")? t + h : t + h + 6;
123 else o.top = (t - dt < ch) ? ((p=="image")? t + h : t + h + 6) : t - ch;
124 if (dw + dl - l >= cw) o.left = l; else o.left = (dw >= cw) ? dw - cw + dl : dl;
125 if (!WebCalendar.timeShow) WebCalendar.dateReg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
126 else WebCalendar.dateReg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
127 try{
128 if (WebCalendar.objExport.value.trim() != ""){
129 WebCalendar.dateStyle = WebCalendar.objExport.value.trim().match(WebCalendar.dateReg);
130 if (WebCalendar.dateStyle == null)
131 {
132 WebCalendar.thisYear = new Date().getFullYear();
133 WebCalendar.thisMonth = new Date().getMonth()+ 1;
134 WebCalendar.thisDay = new Date().getDate();
135 alert("原文本框里的日期有错误!\n可能与你定义的显示时分秒有冲突!");
136 writeCalendar(); return false;
137 }
138 else
139 {
140 WebCalendar.thisYear = parseInt(WebCalendar.dateStyle[1], 10);
141 WebCalendar.thisMonth = parseInt(WebCalendar.dateStyle[3], 10);
142 WebCalendar.thisDay = parseInt(WebCalendar.dateStyle[4], 10);
143 WebCalendar.inputDate = parseInt(WebCalendar.thisDay, 10) +"/"+ parseInt(WebCalendar.thisMonth, 10) +"/"+
144 parseInt(WebCalendar.thisYear, 10); writeCalendar();
145 }
146 } else writeCalendar();
147 } catch(e){writeCalendar();}
148}
149function funMonthSelect() //月份的下拉框
150{
151 var m = isNaN(parseInt(WebCalendar.thisMonth, 10)) ? new Date().getMonth() + 1 : parseInt(WebCalendar.thisMonth);
152 var e = WebCalendar.iframe.document.forms[0].tmpMonthSelect;
153 for (var i=1; i<13; i++) e.options.add(new Option(i +"月", i));
154 e.style.display = ""; e.value = m; e.focus(); window.status = e.style.top;
155}
156function funYearSelect() //年份的下拉框
157{
158 var n = WebCalendar.yearFall;
159 var e = WebCalendar.iframe.document.forms[0].tmpYearSelect;
160 var y = isNaN(parseInt(WebCalendar.thisYear, 10)) ? new Date().getFullYear() : parseInt(WebCalendar.thisYear);
161 y = (y <= 1000)? 1000 : ((y >= 9999)? 9999 : y);
162 var min = (y - n >= 1000) ? y - n : 1000;
163 var max = (y + n <= 9999) ? y + n : 9999;
164 min = (max == 9999) ? max-n*2 : min;
165 max = (min == 1000) ? min+n*2 : max;
166 for (var i=min; i<=max; i++) e.options.add(new Option(i +"年", i));
167 e.style.display = ""; e.value = y; e.focus();
168}
169function prevM() //往前翻月份
170{
171 WebCalendar.thisDay = 1;
172 if (WebCalendar.thisMonth==1)
173 {
174 WebCalendar.thisYear--;
175 WebCalendar.thisMonth=13;
176 }
177 WebCalendar.thisMonth--; writeCalendar();
178}
179function nextM() //往后翻月份
180{
181 WebCalendar.thisDay = 1;
182 if (WebCalendar.thisMonth==12)
183 {
184 WebCalendar.thisYear++;
185 WebCalendar.thisMonth=0;
186 }
187 WebCalendar.thisMonth++; writeCalendar();
188}
189function prevY(){WebCalendar.thisDay = 1; WebCalendar.thisYear--; writeCalendar();}//往前翻 Year
190function nextY(){WebCalendar.thisDay = 1; WebCalendar.thisYear++; writeCalendar();}//往后翻 Year
191function hiddenSelect(e){for(var i=e.options.length; i>-1; i--)e.options.remove(i); e.style.display="none";}
192function getObjectById(id){ if(document.all) return(eval("document.all."+ id)); return(eval(id)); }
193function hiddenCalendar(){getObjectById("meizzCalendarLayer").style.display = "none";};
194function appendZero(n){return(("00"+ n).substr(("00"+ n).length-2));}//日期自动补零程序
195function String.prototype.trim(){return this.replace(/(^\s*)|(\s*$)/g,"");}
196function dayMouseOver()
197{
198 this.className = "over";
199 this.style.backgroundColor = WebCalendar.darkColor;
200 if(WebCalendar.day[this.id.substr(8)].split("/")[1] == WebCalendar.thisMonth)
201 this.style.color = WebCalendar.lightColor;
202}
203function dayMouseOut()
204{
205 this.className = "out"; var d = WebCalendar.day[this.id.substr(8)], a = d.split("/");
206 this.style.removeAttribute('backgroundColor');
207 if(a[1] == WebCalendar.thisMonth && d != WebCalendar.today)
208 {
209 if(WebCalendar.dateStyle && a[0] == parseInt(WebCalendar.dateStyle[4], 10))
210 this.style.color = WebCalendar.lightColor;
211 else
212 this.style.color = WebCalendar.wordColor;
213 }
214}
215function writeCalendar() //对日历显示的数据的处理程序
216{
217 var y = WebCalendar.thisYear;
218 var m = WebCalendar.thisMonth;
219 var d = WebCalendar.thisDay;
220 WebCalendar.daysMonth[1] = (0==y%4 && (y%100!=0 || y%400==0)) ? 29 : 28;
221 if (!(y<=9999 && y >= 1000 && parseInt(m, 10)>0 && parseInt(m, 10)<13 && parseInt(d, 10)>0)){
222 alert("对不起,你输入了错误的日期!");
223 WebCalendar.thisYear = new Date().getFullYear();
224 WebCalendar.thisMonth = new Date().getMonth()+ 1;
225 WebCalendar.thisDay = new Date().getDate(); }
226 y = WebCalendar.thisYear;
227 m = WebCalendar.thisMonth;
228 d = WebCalendar.thisDay;
229 WebCalendar.iframe.meizzYearHead.innerText = y +" 年";
230 WebCalendar.iframe.meizzYearMonth.innerText = parseInt(m, 10) +" 月";
231 WebCalendar.daysMonth[1] = (0==y%4 && (y%100!=0 || y%400==0)) ? 29 : 28; //闰年二月为29天
232 var w = new Date(y, m-1, 1).getDay();
233 var prevDays = m==1 ? WebCalendar.daysMonth[11] : WebCalendar.daysMonth[m-2];
234 for(var i=(w-1); i>=0; i--) //这三个 for 循环为日历赋数据源(数组 WebCalendar.day)格式是 d/m/yyyy
235 {
236 WebCalendar.day[i] = prevDays +"/"+ (parseInt(m, 10)-1) +"/"+ y;
237 if(m==1) WebCalendar.day[i] = prevDays +"/"+ 12 +"/"+ (parseInt(y, 10)-1);
238 prevDays--;
239 }
240 for(var i=1; i<=WebCalendar.daysMonth[m-1]; i++) WebCalendar.day[i+w-1] = i +"/"+ m +"/"+ y;
241 for(var i=1; i<39-w-WebCalendar.daysMonth[m-1]+1; i++)
242 {
243 WebCalendar.day[WebCalendar.daysMonth[m-1]+w-1+i] = i +"/"+ (parseInt(m, 10)+1) +"/"+ y;
244 if(m==12) WebCalendar.day[WebCalendar.daysMonth[m-1]+w-1+i] = i +"/"+ 1 +"/"+ (parseInt(y, 10)+1);
245 }
246 for(var i=0; i<39; i++) //这个循环是根据源数组写到日历里显示
247 {
248 var a = WebCalendar.day[i].split("/");
249 WebCalendar.dayObj[i].innerText = a[0];
250 WebCalendar.dayObj[i].title = a[2] +"-"+ appendZero(a[1]) +"-"+ appendZero(a[0]);
251 WebCalendar.dayObj[i].bgColor = WebCalendar.dayBgColor;
252 WebCalendar.dayObj[i].style.color = WebCalendar.wordColor;
253 if ((i<10 && parseInt(WebCalendar.day[i], 10)>20) || (i>27 && parseInt(WebCalendar.day[i], 10)<12))
254 WebCalendar.dayObj[i].style.color = WebCalendar.wordDark;
255 if (WebCalendar.inputDate==WebCalendar.day[i]) //设置输入框里的日期在日历上的颜色
256 {WebCalendar.dayObj[i].bgColor = WebCalendar.darkColor; WebCalendar.dayObj[i].style.color = WebCalendar.lightColor;}
257 if (WebCalendar.day[i] == WebCalendar.today) //设置今天在日历上反应出来的颜色
258 {WebCalendar.dayObj[i].bgColor = WebCalendar.todayColor; WebCalendar.dayObj[i].style.color = WebCalendar.lightColor;}
259 }
260}
261function returnDate() //根据日期格式等返回用户选定的日期
262{
263 if(WebCalendar.objExport)
264 {
265 var returnValue;
266 var a = (arguments.length==0) ? WebCalendar.day[this.id.substr(8)].split("/") : arguments[0].split("/");
267 var d = WebCalendar.format.match(/^(\w{4})(-|\/)(\w{1,2})\2(\w{1,2})$/);
268 if(d==null){alert("你设定的日期输出格式不对!\r\n\r\n请重新定义 WebCalendar.format !"); return false;}
269 var flag = d[3].length==2 || d[4].length==2; //判断返回的日期格式是否要补零
270 returnValue = flag ? a[2] +d[2]+ appendZero(a[1]) +d[2]+ appendZero(a[0]) : a[2] +d[2]+ a[1] +d[2]+ a[0];
271 if(WebCalendar.timeShow)
272 {
273 var h = new Date().getHours(), m = new Date().getMinutes(), s = new Date().getSeconds();
274 returnValue += flag ? " "+ appendZero(h) +":"+ appendZero(m) +":"+ appendZero(s) : " "+ h +":"+ m +":"+ s;
275 }
276 WebCalendar.objExport.value = returnValue;
277 hiddenCalendar();
278 }
279}
280function document.onclick()
281{
282 if(WebCalendar.eventSrc != window.event.srcElement) hiddenCalendar();
283}
284
285
286
289function makeDate(Obj)
290{
291 var y=eval("document.all."+Obj+"YearInput").value;
292 var m=eval("document.all."+Obj+"MonthInput").value;
293 var d=eval("document.all."+Obj+"DayInput").value;
294 //使用 datetime 数据类型存储从 1753 年 1 月 1 日至 9999 年 12 月 31 日的日期
295 if(isDate(y,m,d) && y>=1753)
296 {
297 eval("document.all."+Obj).value=y+"-"+m+"-"+d;
298 }
299 else
300 {

这只是一部分 一次没法回答完
全部回答
去http://dojotoolkit.org。按照例子嵌入一个标签就搞定了。没必要自己写。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
鞍山限号吗
核桃夹枣自己可以做吗,做枣夹核桃用什么枣是
锅炉运行时如何调整负荷
一个人在水边看鱼,清水,有三条鱼,打一成语
邓丽君的人品真那么好吗
为什么移动硬盘盘能在win7上读出来,到win10
广西的桂林白米糕是热性食物吗
求古代言情短篇小说
42CrMO4 N是哪个材料?
htc 10怎么进twrp recovery
梧州新海燃气有限公司地址有知道的么?有点事
显卡温度70度左右正常嘛?
英格尔化学和微普化学那家靠谱
下列解说不恰当的一项是A.“文房四宝”指“笔
水泥路面铺二灰返砂什么原因
推荐资讯
特色美容美发部地址有知道的么?有点事想过去
冲脑门口香糖为什么没有太多的广告
cad考试高悬赏!
复姓说三个名字的前两个字是姓后一个字是名,
母猪还没有下完仔,可以让先下的小猪吃奶吗?
王中山这三个字英文名怎么读
黄草就是铁皮枫斗吗
Apple watch几个g?系统占了多少?
广州去清远薰衣草世界好玩怎么坐车
北朝鲜女的现在被延吉龙井拘留所抓了咋样能救
中超球队没有用过韩国亚外的是不是只有国安
三开四孔开关怎么控制三个灯?就这个开关怎么
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?