动态万年历 通过魔法字符串给整个table添加组件 通过函数获取当前日期,保存到数组中 遍历数组,添加日期 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> body { margin: 0; } #box { width: 600px; margin: 50px auto; border: 1px solid blue; } #box header { height: 50px; line-height: 50px; text-align: center; color: #fff; background-color: #17a; padding: 0 10px; } #box header span { cursor: pointer; } #box header .fl { float: left; } #box header .fr { float: right; } #box header div { font-size: 30px; } #box table { border-right: 1px solid #ccc; border-bottom: 1px solid #ccc; } #box table th, #box table td { border-left: 1px solid #ccc; border-top: 1px solid #ccc; text-align: center; height: 30px; } </style></head><body> <div id="box"> <!-- <header> <span class="fl">上个月</span> <span class="fr">下个月</span> <div>2022年10月</div> </header> <table width="100%" cellspacing="0" cellpadding="0"> <thead> <tr> <th class="red">周日</th> <th>周一</th> <th>周二</th> <th>周三</th> <th>周四</th> <th>周五</th> <th>周六</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> </tbody> </table> --> </div></body><script src="../jquery.min.js"></script><script> $(function () { var defaultDate = new Date(2024, 0); var box = $("#box"); var yearMonthTitleDiv = null; var prevMonthSpan = null; var nextMonthSpan = null; var tBody = null; var allTd = []; //创建 create(); //显示当前对应的日期 showDate(); function create() { var header = $(` <header> <span class="fl">上个月</span> <span class="fr">下个月</span> <div>2022年10月</div> </header> `); box.append(header); yearMonthTitleDiv = header.find('div'); prevMonthSpan = header.find('.fl'); nextMonthSpan = header.find('.fr'); var table = $(` <table width="100%" cellspacing="0" cellpadding="0"> <thead> <tr> <th class="red">周日</th> <th>周一</th> <th>周二</th> <th>周三</th> <th>周四</th> <th>周五</th> <th>周六</th> </tr> </thead> <tbody> </table> `); box.append(table) tBody = table.find('tbody') for (var i = 0; i < 6; i++) { var tr = $("<tr></tr>"); for (var k = 0; k < 7; k++) { var td = $("<td></td>"); tr.append(td); allTd.push(td); } tBody.append(tr) } } function showDate() { var year = defaultDate.getFullYear(); var month = defaultDate.getMonth() + 1; // console.log(month); yearMonthTitleDiv.text(`${year}年${month}月`); var week = new Date(year, month - 1, 1).getDay();//一号是星期几 var days = new Date(year, month, 0).getDate();//当前月的前一个月的最后一天是几号 var n = 1; for (var i = 0; i < allTd.length; i++) { allTd[i].empty(); if (i >= week && n <= days) { allTd[i].text(n); n++; } } if (allTd[28].text() === '') { allTd[28].parent().hide(); allTd[35].parent().hide(); } else if (allTd[35].text() === '') { allTd[35].parent().hide(); } else { allTd[35].parent().show(); allTd[28].parent().show(); } } $('#box span').click(function () { var year = defaultDate.getFullYear(); var month = defaultDate.getMonth(); if ($(this).index() == 0) { month--; } else { month++; } defaultDate = new Date(year, month); showDate(); }) })</script></html>