It is mainly divided into three parts, 1: Obtaining the li element 2: How to fill in the corresponding date 3: How to obtain the event of clicking the li element.
1: Obtain the li element through the children attribute of the relationship between nodes (two for loop traversals);
2: When traversing to fill in the date, in the first The row determines what day of the week the first day of the month falls on, and then starts filling in from the first day, until it is greater than the number of days in the current month, it stops filling in, and starts filling in the date of the next month. The first row displays the date of last month: the number of days in the previous month displays the starting value = calculates the number of days in the last month - the value of the day of the week on the first day of this month - 1, and then displays the starting value of the number of days in the previous month in the first row Add by yourself.
3: Use JS event bubbling to obtain the innerHTML of li and display the corresponding date
Rendering:
The code is as follows:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>calendar</title> <style> .clear:after{ content:""; display:table; clear:both; } .left{ float:left; } ul{ padding:0px; margin-top:5px; margin-bottom:0px; } ul>li{ float:left; list-style:none; width:30px; height: 21px; border: 1px solid #ccc; text-align:center; } .gray{ color:#766565; } .top { height:25px; } .top .lf-tri{ border:10px solid transparent; border-right-color:black; margin-top:4px; } .top .rf-tri{ border:10px solid transparent; border-left-color:black; margin-top:4px; } .top .content{ width:185px; height:5px; text-align:center; } </style> </head> <body> <p class="top clear"> <p class="left lf-tri" onclick="lastMonth()"></p> <p class="left content">2017年2月1日</p> <p class="left rf-tri" onclick=" nextMonth()"></p> </p> <p> <p id="week"> <ul class="clear"> <li>日</li> <li>一</li> <li>二</li> <li>三</li> <li>四</li> <li>五</li> <li>六</li> <ul> </p> <p id="date" onclick='getDateNum(event)'> <ul class="clear"> <li>30</li> <li>31</li> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> <ul class="clear"> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> <li>11</li> <li>12</li> </ul> <ul class="clear"> <li>13</li> <li>14</li> <li>15</li> <li>16</li> <li>17</li> <li>18</li> <li>19</li> </ul> <ul class="clear"> <li>20</li> <li>21</li> <li>22</li> <li>23</li> <li>24</li> <li>25</li> <li>26</li> </ul> <ul class="clear"> <li>27</li> <li>28</li> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> <ul class="clear"> <li>27</li> <li>28</li> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </p> <p> <script> function $(id){ return document.getElementById(id); } function change(cls){ return document.getElementsByClassName(cls); } function getDateNum(e) { console.log(e && e.target.nodeName)//打印发生事件的元素,再获取元素nodeName if(e.target.nodeName=="LI"){//先判断nodeName是LI if(e.target.className!="gray"){//点击本月的日期,显示在日期栏 change("content")[0].innerHTML = year+'年'+(month+1)+'月'+e.target.innerHTML+'日'; }else{//点击灰色日期即(上个月或者下个月日期)切换到当月 if(e.target.innerHTML>14){ lastMonth(); }else { nextMonth(); } } } } //获取年、月 var today = new Date(); var year=today.getFullYear(), month = today.getMonth(); var totalDay; var uls=$("date").children,list; function loadCalendar(){ totalDay=getMonthDays(year,month+1);//计算一个月的天数 var firstDay = (new Date(year,month,1)).getDay();//计算每个月1号在星期几 var lastMonthDay=getMonthDays(year,month); var lastDayCal=lastMonthDay-firstDay+1;//计算上个月在第一行显示的天数 //获取ul元素 var num=1 ,nextNum=1;//日期显示 // 类数组对象 转 数组 //uls = Array.prototype.slice.call(uls) //获取li元素 填充 for(var r=0;r<uls.length;r++){ list=uls[r].children;//遍历ul,获得li for(var line=0;line<list.length;line++){ if(r==0){//在第一行 与第一天进行判断 大于等于第一天时载入日期 if(line>=firstDay){ list[line].innerHTML=num++; list[line].setAttribute("class",""); }else { list[line].innerHTML=lastDayCal++;//第一行的上个月天数显示 list[line].setAttribute("class","gray"); } }else { //判断是否超出天数 ,不超出则继续加,超出则显示下个月日期 if(num<=totalDay){ list[line].setAttribute("class",""); list[line].innerHTML=num++; }else { list[line].innerHTML=nextNum++;//下个月日期显示 list[line].setAttribute("class","gray"); } } } } } loadCalendar(); function getMonthDays(year,month){ //判断2月份天数 if(month==2){ days= (year%4==0)&&(year%100!=0)||(year%400==0)? 29:28; }else { //1-7月 单数月为31日 if(month<7){ days= month%2==1?31:30; }else { //8-12月 双月为31日 days = month%2==0?31:30; } } return days; } //右击箭头下个月 //change("rf-tri")[0].onclick = function nextMonth() { month++; if(month>11){ year+=1; month=0; } change("content")[0].innerHTML=year+"年"+(month+1)+"月"+"1日"; //console.log(month+1); loadCalendar(); } //左击箭头上个月 //change("lf-tri")[0].onclick = function lastMonth() { month--; if(month<0){ month=11; year-=1; } change("content")[0].innerHTML=year+"年"+(month+1)+"月"+"1日"; //console.log(month+1); loadCalendar(); } </script> </body> </html>
For more articles related to JS implementing a simple calendar, please pay attention to the PHP Chinese website!

JavaandJavaScriptaredistinctlanguages:Javaisusedforenterpriseandmobileapps,whileJavaScriptisforinteractivewebpages.1)Javaiscompiled,staticallytyped,andrunsonJVM.2)JavaScriptisinterpreted,dynamicallytyped,andrunsinbrowsersorNode.js.3)JavausesOOPwithcl

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
