效果图
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#calendar {
background-color: #9900ff;
color: #fff;
border-radius: 5px;
margin: 100px auto;
}
#title {
font-size: 1.4em;
padding: 4px 0.55em;
}
#days th {
font-weight: bold;
text-align: center;
padding: 4px 0.55em;
}
#calendar td {
text-align: center;
padding: 4px 20px;
}
#today {
color: #f00;
font-weight: bold;
}
.poin {
cursor: pointer;
cursor: hand;
}
</style>
<script>
window.onload=function(){
var form = document.getElementById('calendar');
calendar.init(form);
}
var calendar = {
year: null,
month: null,
dayTable: null,
init(form) {
this.dayTable=form.getElementsByTagName('td');
this.createCalendar(form,new Date());
var nextMon=form.getElementsByTagName('th')[2];
var preMon=form.getElementsByTagName('th')[0];
preMon.onclick=function(){
calendar.clearCalendar(form)
var preDate=new Date(calendar.year,calendar.month-1,1);
calendar.createCalendar(form,preDate)
}
nextMon.onclick=function(){
calendar.clearCalendar(form)
var nextDate=new Date(calendar.year,calendar.month+1,1);
calendar.createCalendar(form,nextDate)
}
},
clearCalendar(form){
var tds=form.getElementsByTagName('td');
for (var i = 0; i < tds.length; i++) {
tds[i].innerHTML=' ';
tds[i].id='';
}
},
createCalendar(form,date){
this.year=date.getFullYear();
this.month=date.getMonth();
form.getElementsByTagName('th')[1].innerHTML = this.year+"年"+(this.month+1)+"月";
var dataNum=this.getDateLen(this.year,this.month);
var fristDay = this.getFristDay(calendar.year,calendar.month);
for (var i = 1; i <= dataNum; i++) {
this.dayTable[fristDay+i-1].innerHTML=i;
var nowDate =new Date();
if(i ==nowDate.getDate() && calendar.month ==nowDate.getMonth()&&calendar.year == nowDate.getFullYear()){
calendar.dayTable[i+fristDay-1].id = "today";
}
}
},
getDateLen(year,month){
var nextMonth=new Date(year,month+1,1);
nextMonth.setHours(nextMonth.getHours()-1);
return nextMonth.getDate();
},
getFristDay:function(year,month){
var fristDay=new Date(year,month,1);
return fristDay.getDay();
}
}
</script>
</head>
<body>
<table id="calendar">
<tr>
<th class="poin"><<</th>
<th id="title" colspan="5"></th>
<th class="poin">>></th>
</tr>
<tr id="days">
<th>日</th>
<th>一</th>
<th>二</th>
<th>三</th>
<th>四</th>
<th>五</th>
<th>六</th>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>