欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaScript实现简单的日历

程序员文章站 2023-12-21 18:34:29
...
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Js日历</title>
  6. <style type="text/css">
  7. .evrday {
  8. width: 14%;
  9. float: left;
  10. }
  11. #calendar {
  12. width: 400px;
  13. margin: auto;
  14. }
  15. button {
  16. width: 25%;
  17. height: 40px;
  18. float: left;
  19. }
  20. div {
  21. margin-top: 10px;
  22. margin-bottom: 5px;
  23. text-align: center;
  24. }
  25. #month {
  26. width: 50%;
  27. float: left;
  28. }
  29. </style>
  30. </head>
  31. <body onLoad="showday()">
  32. <div id="calendar">
  33. <div>
  34. <button onClick="shang()">上个月</button>
  35. <div id="month"></div>
  36. <button onClick="next()">下个月</button>
  37. </div>
  38. <div>
  39. <div class="evrday"></div>
  40. <div class="evrday"></div>
  41. <div class="evrday"></div>
  42. <div class="evrday"></div>
  43. <div class="evrday"></div>
  44. <div class="evrday"></div>
  45. <div class="evrday"></div>
  46. </div>
  47. <div id="day"></div>
  48. </div>
  49. <script>
  50. var today = new Date();
  51. var year = today.getFullYear();
  52. var month = today.getMonth() + 1;
  53. var day = today.getDate();
  54. var allday = 0;
  55. function showmonth() {
  56. var year_month = year + "年" + month + "月" + day + "日";
  57. document.getElementById("month").innerHTML = year_month;
  58. }
  59. function count() {
  60. if (month != 2) {
  61. if (month == 4 || month == 6 || month == 9 || month == 11)
  62. allday = 30;
  63. else
  64. allday = 31;
  65. } else {
  66. if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
  67. allday = 29;
  68. else
  69. allday = 28;
  70. }
  71. }
  72. function showday() {
  73. showmonth()
  74. count();
  75. var fistdate = new Date(year, month - 1, 1);
  76. var xinqi = fistdate.getDay();
  77. var daterow = document.getElementById("day");
  78. for (var i = 0; i < xinqi; i++)
  79. {
  80. var dayElement = document.createElement("div");
  81. dayElement.className = "evrday";
  82. daterow.appendChild(dayElement);
  83. }
  84. for (var j = 1; j <= allday; j++) {
  85. var dayElement = document.createElement("div");
  86. dayElement.innerHTML = j;
  87. dayElement.className = "evrday";
  88. if (j == day)
  89. dayElement.style.color = "red";
  90. daterow.appendChild(dayElement);
  91. }
  92. }
  93. //下个月
  94. function next() {
  95. document.getElementById('day').innerHTML = "";
  96. if (month < 12) {
  97. month = month + 1;
  98. } else {
  99. month = 1;
  100. year = year + 1;
  101. }
  102. document.getElementById('div').innerHTML = showday();
  103. }
  104. //上个月
  105. function shang() {
  106. document.getElementById('day').innerHTML = "";
  107. if (month > 1) {
  108. month = month - 1;
  109. } else {
  110. month = 12;
  111. year = year - 1;
  112. }
  113. document.getElementById('div').innerHTML = showday();
  114. }
  115. </script>
  116. </body>
  117. </html>

效果图

JavaScript实现简单的日历
就是不知道这个报错是怎么回事。。。
另外想实现回到今天的功能,想了一会,发现没思路。。。。试了几次都是BUG,就。。。就算了。。。JavaScript实现简单的日历

上一篇:

下一篇: