本文实例为大家分享了js实现日历效果的具体代码,供大家参考,具体内容如下
html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>日历插件</title>
<link rel="stylesheet" href="./css/reset.css" >
<link rel="stylesheet" href="./css/index.css" >
</head>
<body>
<div class="show">
<button id="pre">上月</button>
<span id="showtime">2019-01</span>
<button id="next">下月</button>
</div>
<div class="box" id="box">
</div>
</body>
<script>
// 获取本月第一天
function getMonthDay(date){
date=new Date(date.valueOf())
date.setDate(1);
return date
}
// 获取本月最后一天
function getMonthLastDay(date){
date=new Date(date.valueOf())
date.setMonth(date.getMonth()+1);
date.setDate(0);
return date
}
//获取本月的时间对象集合
function getMonth(date){
var arr=[]
// 获取本月第一天
var _date=getMonthDay(date)
// // 获取本月最后一天
var dataLast=getMonthLastDay(date).getDate()
arr.push(new Date(_date.valueOf()))
// 处理本月第一天 到本月最后一天
for(var i =1;i<dataLast;i++){
_date.setDate(_date.getDate()+1)
arr.push(new Date(_date.valueOf()))
}
// 向前补全,重置为本月一号
_date=getMonthDay(date)
var forln=_date.getDay()
for(var i=0;i<forln;i++){
_date.setDate(_date.getDate()-1)
arr.unshift(new Date(_date.valueOf()))
}
// 向后补全,重置为本月最后一天
_date=getMonthLastDay(date)
forln=_date.getDay()
for(var i=forln;i<6;i++){
_date.setDate(_date.getDate()+1)
arr.push(new Date(_date.valueOf()))
}
return arr
}
// 将集合渲染到页面
function renderTable(monthDateArr,date){
document.getElementById("showtime").innerHTML=`${date.getFullYear()}-${date.getMonth()+1}`
var table=document.createElement('table')
var trTh=document.createElement('tr')
trTh.innerHTML=`
<th>周日</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
`
table.appendChild(trTh)
var tr=document.createElement('tr')
for(let i in monthDateArr){
var td=document.createElement('td')
td.innerHTML=monthDateArr[i].getDate()
tr.appendChild(td)
if((i*1+1)%7===0){
table.appendChild(tr)
tr=document.createElement('tr')
}else if(i == monthDateArr.length-1){
table.appendChild(tr)
}
}
document.getElementById("box").innerHTML=table.outerHTML
}
var date=new Date()
document.getElementById('pre').onclick=function(){
date.setDate(1)
date.setMonth(date.getMonth()-1)
renderTable(getMonth(date),date)
}
document.getElementById('next').onclick=function(){
date.setDate(1)
date.setMonth(date.getMonth()+1)
rrenderTable(getMonth(date),date)
}
renderTable(getMonth(date),date)
</script>
</html>
css代码
.box{
width: 700px;
margin: 0 auto;
box-sizing: border-box;
padding: 0 1px;
}
table{
width: 100%;
}
th,td{
width:100px;
text-align: center;
}
th{
height: 30px;
line-height: 30px;
background: #e0e5e5;
}
td{
height: 70px;
line-height: 70px;
background: #f3f5f5;
}
.show{
display: flex;
align-items: center;
justify-content: center;
}
更多精彩内容请点击专题《javascript日历插件使用方法汇总》进行学习
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
js,日历
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“原生js实现日历效果”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
2025年11月01日
2025年11月01日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]