js - 日历控件(1)

  1. <!--
  2. /**
  3. * 返回日期
  4. * @param d the delimiter
  5. * @param p the pattern of your date
  6. */
  7. String.prototype.toDate = function(x, p) {
  8. if(x == null) x = "-";
  9. if(p == null) p = "ymd";
  10. var a = this.split(x);
  11. var y = parseInt(a[p.indexOf("y")]);
  12. //remember to change this next century ;)
  13. if(y.toString().length <= 2) y += 2000;
  14. if(isNaN(y)) y = new Date().getFullYear();
  15. var m = parseInt(a[p.indexOf("m")]) - 1;
  16. var d = parseInt(a[p.indexOf("d")]);
  17. if(isNaN(d)) d = 1;
  18. return new Date(y, m, d);
  19. }
  20. /**
  21. * 格式化日期
  22. * @param   d the delimiter
  23. * @param   p the pattern of your date
  24. * @author meizz
  25. */
  26. Date.prototype.format = function(style) {
  27. var o = {
  28. "M+" : this.getMonth() + 1, //month
  29. "d+" : this.getDate(),      //day
  30. "h+" : this.getHours(),     //hour
  31. "m+" : this.getMinutes(),   //minute
  32. "s+" : this.getSeconds(),   //second
  33. "w+" : "天一二三四五六".charAt(this.getDay()),   //week
  34. "q+" : Math.floor((this.getMonth() + 3) / 3), //quarter
  35. "S" : this.getMilliseconds() //millisecond
  36. }
  37. if(/(y+)/.test(style)) {
  38. style = style.replace(RegExp.$1,
  39. (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  40. }
  41. for(var k in o){
  42. if(new RegExp("("+ k +")").test(style)){
  43. style = style.replace(RegExp.$1,
  44. RegExp.$1.length == 1 ? o[k] :
  45. ("00" + o[k]).substr(("" + o[k]).length));
  46. }
  47. }
  48. return style;
  49. };
  50. /**
  51. * 日历类
  52. * @param   beginYear 1990
  53. * @param   endYear   2010
  54. * @param   lang      0(中文)|1(英语) 可自由扩充
  55. * @param   dateFormatStyle "yyyy-MM-dd";
  56. * @version 2006-04-01
  57. * @author KimSoft (jinqinghua [at] gmail.com)
  58. * @update
  59. */
  60. function Calendar(beginYear, endYear, lang, dateFormatStyle) {
  61. this.beginYear = 1990;
  62. this.endYear = 2010;
  63. this.lang = 0;            //0(中文) | 1(英文)
  64. this.dateFormatStyle = "yyyy-MM-dd";
  65. if (beginYear != null && endYear != null){
  66. this.beginYear = beginYear;
  67. this.endYear = endYear;
  68. }
  69. if (lang != null){
  70. this.lang = lang
  71. }
  72. if (dateFormatStyle != null){
  73. this.dateFormatStyle = dateFormatStyle
  74. }
  75. this.dateControl = null;
  76. this.panel = this.getElementById("calendarPanel");
  77. this.form = null;
  78. this.date = new Date();
  79. this.year = this.date.getFullYear();
  80. this.month = this.date.getMonth();
  81. this.colors = {
  82. "cur_word"      : "#FFFFFF"//当日日期文字颜色
  83. "cur_bg"        : "#00FF00"//当日日期单元格背影色
  84. "sun_word"      : "#FF0000"//星期天文字颜色
  85. "sat_word"      : "#0000FF"//星期六文字颜色
  86. "td_word_light" : "#333333"//单元格文字颜色
  87. "td_word_dark" : "#CCCCCC"//单元格文字暗色
  88. "td_bg_out"     : "#EFEFEF"//单元格背影色
  89. "td_bg_over"    : "#FFCC00"//单元格背影色
  90. "tr_word"       : "#FFFFFF"//日历头文字颜色
  91. "tr_bg"         : "#666666"//日历头背影色
  92. "input_border" : "#CCCCCC"//input控件的边框颜色
  93. "input_bg"      : "#EFEFEF"   //input控件的背影色
  94. }
  95. this.draw();
  96. this.bindYear();
  97. this.bindMonth();
  98. this.changeSelect();
  99. this.bindData();
  100. }
  101. /**
  102. * 日历类属性(语言包,可自由扩展)
  103. */
  104. Calendar.language = {
  105. "year"   : [[""], [""]],
  106. "months" : [["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
  107. ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]
  108. ],
  109. "weeks" : [["日","一","二","三","四","五","六"],
  110. ["SUN","MON","TUR","WED","THU","FRI","SAT"]
  111. ],
  112. "clear" : [["清空"], ["CLS"]],
  113. "today" : [["今天"], ["TODAY"]],
  114. "close" : [["关闭"], ["CLOSE"]]
  115. }
  116. Calendar.prototype.draw = function() {
  117. calendar = this;
  118. var mvAry = [];
  119. mvAry[mvAry.length] = ' <form name="calendarForm" style="margin: 0px;">';
  120. mvAry[mvAry.length] = '    <table width="100%" border="0" cellpadding="0" cellspacing="1">';
  121. mvAry[mvAry.length] = '      <tr>';
  122. 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>';
  123. mvAry[mvAry.length] = '        <th align="center" width="98%" nowrap="nowrap"><select name="calendarYear" id="calendarYear" style="font-size:12px;"></select><select name="calendarMonth" id="calendarMonth" style="font-size:12px;"></select></th>';
  124. 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>';
  125. mvAry[mvAry.length] = '      </tr>';
  126. mvAry[mvAry.length] = '    </table>';
  127. mvAry[mvAry.length] = '    <table id="calendarTable" width="100%" style="border:0px solid #CCCCCC;background-color:#FFFFFF" border="0" cellpadding="3" cellspacing="1">';
  128. mvAry[mvAry.length] = '      <tr>';
  129. for(var i = 0; i < 7; i++) {
  130. 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>';
  131. }
  132. mvAry[mvAry.length] = '      </tr>';
  133. for(var i = 0; i < 6;i++){
  134. mvAry[mvAry.length] = '    <tr align="center">';
  135. for(var j = 0; j < 7; j++) {
  136. if (j == 0){
  137. mvAry[mvAry.length] = ' <td style="cursor:default;color:' + calendar.colors["sun_word"] + ';"></td>';
  138. else if(j == 6) {
  139. mvAry[mvAry.length] = ' <td style="cursor:default;color:' + calendar.colors["sat_word"] + ';"></td>';
  140. else {
  141. mvAry[mvAry.length] = ' <td style="cursor:default;"></td>';
  142. }
  143. }
  144. mvAry[mvAry.length] = '    </tr>';
  145. }
  146. mvAry[mvAry.length] = '      <tr style="background-color:' + calendar.colors["input_bg"] + ';">';
  147. 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>';
  148. 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>';
  149. 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>';
  150. mvAry[mvAry.length] = '      </tr>';
  151. mvAry[mvAry.length] = '    </table>';
  152. mvAry[mvAry.length] = ' </form>';
  153. this.panel.innerHTML = mvAry.join("");
  154. this.form = document.forms["calendarForm"];
  155. this.form.prevMonth.onclick = function () {calendar.goPrevMonth(this);}
  156. this.form.nextMonth.onclick = function () {calendar.goNextMonth(this);}
  157. this.form.calendarClear.onclick = function () {calendar.dateControl.value = "";calendar.hide();}
  158. this.form.calendarClose.onclick = function () {calendar.hide();}
  159. this.form.calendarYear.onchange = function () {calendar.update(this);}
  160. this.form.calendarMonth.onchange = function () {calendar.update(this);}
  161. this.form.calendarToday.onclick = function () {
  162. var today = new Date();
  163. calendar.date = today;
  164. calendar.year = today.getFullYear();
  165. calendar.month = today.getMonth();
  166. calendar.changeSelect();
  167. calendar.bindData();
  168. calendar.dateControl.value = today.format(calendar.dateFormatStyle);
  169. calendar.hide();
  170. }
  171. }
  172. //年份下拉框绑定数据
  173. Calendar.prototype.bindYear = function() {
  174. var cy = this.form.calendarYear;
  175. cy.length = 0;
  176. for (var i = this.beginYear; i <= this.endYear; i++){
  177. cy.options[cy.length] = new Option(i + Calendar.language["year"][this.lang], i);
  178. }
  179. }
  180. //月份下拉框绑定数据
  181. Calendar.prototype.bindMonth = function() {
  182. var cm = this.form.calendarMonth;
  183. cm.length = 0;
  184. for (var i = 0; i < 12; i++){
  185. cm.options[cm.length] = new Option(Calendar.language["months"][this.lang][i], i);
  186. }
  187. }
  188. //向前一月
  189. Calendar.prototype.goPrevMonth = function(e){
  190. if (this.year == this.beginYear && this.month == 0){return;}
  191. this.month--;
  192. if (this.month == -1) {
  193. this.year--;
  194. this.month = 11;
  195. }
  196. this.date = new Date(this.year, this.month, 1);
  197. this.changeSelect();
  198. this.bindData();
  199. }
  200. //向后一月
  201. Calendar.prototype.goNextMonth = function(e){
  202. if (this.year == this.endYear && this.month == 11){return;}
  203. this.month++;
  204. if (this.month == 12) {
  205. this.year++;
  206. this.month = 0;
  207. }
  208. this.date = new Date(this.year, this.month, 1);
  209. this.changeSelect();
  210. this.bindData();
  211. }
  212. //改变SELECT选中状态
  213. Calendar.prototype.changeSelect = function() {
  214. var cy = this.form.calendarYear;
  215. var cm = this.form.calendarMonth;
  216. for (var i= 0; i < cy.length; i++){
  217. if (cy.options[i].value == this.date.getFullYear()){
  218. cy[i].selected = true;
  219. break;
  220. }
  221. }
  222. for (var i= 0; i < cm.length; i++){
  223. if (cm.options[i].value == this.date.getMonth()){
  224. cm[i].selected = true;
  225. break;
  226. }
  227. }
  228. }
  229. //更新年、月
  230. Calendar.prototype.update = function (e){
  231. this.year = e.form.calendarYear.options[e.form.calendarYear.selectedIndex].value;
  232. this.month = e.form.calendarMonth.options[e.form.calendarMonth.selectedIndex].value;
  233. this.date = new Date(this.year, this.month, 1);
  234. this.changeSelect();
  235. this.bindData();
  236. }
  237. //绑定数据到月视图
  238. Calendar.prototype.bindData = function () {
  239. var calendar = this;
  240. var dateArray = this.getMonthViewArray(this.date.getYear(), this.date.getMonth());
  241. var tds = this.getElementById("calendarTable").getElementsByTagName("td");
  242. for(var i = 0; i < tds.length; i++) {
  243. //tds[i].style.color = calendar.colors["td_word_light"];
  244. tds[i].style.backgroundColor = calendar.colors["td_bg_out"];
  245. tds[i].onclick = function () {return;}
  246. tds[i].onmouseover = function () {return;}
  247. tds[i].onmouseout = function () {return;}
  248. if (i > dateArray.length - 1) break;
  249. tds[i].innerHTML = dateArray[i];
  250. if (dateArray[i] != " "){
  251. tds[i].onclick = function () {
  252. if (calendar.dateControl != null){
  253. calendar.dateControl.value = new Date(calendar.date.getFullYear(),
  254. calendar.date.getMonth(),
  255. this.innerHTML).format(calendar.dateFormatStyle);
  256. }
  257. calendar.hide();
  258. }
  259. tds[i].onmouseover = function () {
  260. this.style.backgroundColor = calendar.colors["td_bg_over"];
  261. }
  262. tds[i].onmouseout = function () {
  263. this.style.backgroundColor = calendar.colors["td_bg_out"];
  264. }
  265. if (new Date().format(calendar.dateFormatStyle) ==
  266. new Date(calendar.date.getFullYear(),
  267. calendar.date.getMonth(),
  268. dateArray[i]).format(calendar.dateFormatStyle)) {
  269. //tds[i].style.color = calendar.colors["cur_word"];
  270. tds[i].style.backgroundColor = calendar.colors["cur_bg"];
  271. tds[i].onmouseover = function () {
  272. this.style.backgroundColor = calendar.colors["td_bg_over"];
  273. }
  274. tds[i].onmouseout = function () {
  275. this.style.backgroundColor = calendar.colors["cur_bg"];
  276. }
  277. }//end if
  278. }
  279. }
  280. }
  281. //根据年、月得到月视图数据(数组形式)
  282. Calendar.prototype.getMonthViewArray = function (y, m) {
  283. var mvArray = [];
  284. var dayOfFirstDay = new Date(y, m, 1).getDay();
  285. var daysOfMonth = new Date(y, m + 1, 0).getDate();
  286. for (var i = 0; i < 42; i++) {
  287. mvArray[i] = " ";
  288. }
  289. for (var i = 0; i < daysOfMonth; i++){
  290. mvArray[i + dayOfFirstDay] = i + 1;
  291. }
  292. return mvArray;
  293. }
  294. //扩展 document.getElementById(id) 多浏览器兼容性 from meizz tree source
  295. Calendar.prototype.getElementById = function(id){
  296. if (typeof(id) != "string" || id == ""return null;
  297. if (document.getElementById) return document.getElementById(id);
  298. if (document.all) return document.all(id);
  299. try {return eval(id);} catch(e){ return null;}
  300. }
  301. //扩展 object.getElementsByTagName(tagName)
  302. Calendar.prototype.getElementsByTagName = function(object, tagName){
  303. if (document.getElementsByTagName) return document.getElementsByTagName(tagName);
  304. if (document.all) return document.all.tags(tagName);
  305. }
  306. //取得HTML控件绝对位置
  307. Calendar.prototype.getAbsPoint = function (e){
  308. var x = e.offsetLeft;
  309. var y = e.offsetTop;
  310. while(e = e.offsetParent){
  311. x += e.offsetLeft;
  312. y += e.offsetTop;
  313. }
  314. return {"x": x, "y": y};
  315. }
  316. //显示日历
  317. Calendar.prototype.show = function (dateControl, popControl) {
  318. if (dateControl == null){
  319. throw new Error("arguments[0] is necessary")
  320. }
  321. this.dateControl = dateControl;
  322. if (dateControl.value.length > 0){
  323. this.date = new Date(dateControl.value.toDate());
  324. this.year = this.date.getFullYear();
  325. this.month = this.date.getMonth();
  326. this.changeSelect();
  327. this.bindData();
  328. }
  329. if (popControl == null){
  330. popControl = dateControl;
  331. }
  332. var xy = this.getAbsPoint(popControl);
  333. this.panel.style.left = xy.x + "px";
  334. this.panel.style.top = (xy.y + dateControl.offsetHeight) + "px";
  335. this.setDisplayStyle("select""hidden");
  336. this.panel.style.visibility = "visible";
  337. }
  338. //隐藏日历
  339. Calendar.prototype.hide = function() {
  340. this.setDisplayStyle("select""visible");
  341. this.panel.style.visibility = "hidden";
  342. }
  343. //设置控件显示或隐藏
  344. Calendar.prototype.setDisplayStyle = function(tagName, style) {
  345. var tags = this.getElementsByTagName(null, tagName)
  346. for(var i = 0; i < tags.length; i++) {
  347. if (tagName.toLowerCase() == "select" &
  348. (tags[i].name == "calendarYear" ||
  349. tags[i].name == "calendarMonth")){
  350. continue;
  351. }
  352. tags[i].style.visibility = style;
  353. }
  354. }
  355. document.write('<div id="calendarPanel" style="position: absolute;visibility: hidden;z-index: 9999;background-color: #FFFFFF;border: 1px solid #CCCCCC;width:175px;font-size:12px;"></div>');
  356. var calendar = new Calendar();
  357. //调用calendar.show(dateControl, popControl);
  358. //-->
  359. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  360. <html>
  361.     <head>
  362.         <title>Web Calender Control</title>
  363.         <script language="javascript" type="text/javascript" src="WebCalendar.js"></script>
  364.     </head>
  365.     <body>
  366.         <form>
  367.             <input name="date" type="text" id="date" οnclick="new Calendar().show(this);" size="10" maxlength="10" readonly="readonly"/>
  368.             <input name="date" type="text" id="date1" οnclick="new Calendar(null, null, 1).show(this);" />
  369.         </form>
  370.     </body>
  371. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值