JavaScript实现年历效果
作者:layroy 时间:2023-09-10 10:53:26
本文实例为大家分享了JavaScript实现年历效果的具体代码,供大家参考,具体内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>制作年历</title>
<style>
body{text-align:center;}
.box{margin:0 auto;width:880px;}
.title{background: #ccc;}
table{height:200px;width:200px;font-size:12px;text-align:center;float:left;margin:10px;font-family:arial;}
</style>
<script src="calendar.js"></script>
<script>
var year = parseInt(prompt('输入年份:','2019'));//制作弹窗
document.write(calendar(year));//调用函数生成指定年份的年历
</script>
</head>
<body>
</body>
</html>
calendar.js
function calendar(y){
//获取指定年份1月1日的星期数值
var w = new Date(y,0).getDay();
var html = '<div class="box">';
//拼接每个月份的表格
for(m=1;m<=12;m++){
html += '<table>';
html += '<tr class="title"><th colspan="7">' + y + '年' +m+' 月</th></tr>';
html += '<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>'
//获取每个月份共有多少天
var max = new Date(y,m,0).getDate();
html += '<tr>';//开始<tr>标签
for (d=1;d<=max;d++){
if(w && d== 1){//如果该月的第1天不是星期日,则填充空白
html += '<td colspan ="' + w + '"> </td>';
}
html += '<td>' +d+ '</td>';
if(w == 6 && d != max){//如果星期六不是该月的最后一天,则换行
html += '</tr><tr>';
}else if(d==max){//该月的最后一天,闭合</tr>标签
html += '</tr>';
}
w = (w+1>6) ? 0 : w+1;
}
html += '</table>';
}
html += '</div>';
return html;
}
效果
来源:https://blog.csdn.net/m0_57202083/article/details/119849337
标签:js,年历
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Python进阶之切片的误区与高级用法
2022-09-18 04:03:12
python图片指定区域替换img.paste函数的使用
2023-10-17 02:34:01
![](https://img.aspxhome.com/file/2023/3/131303_0s.jpg)
Python实现统计代码行的方法分析
2023-10-15 13:08:44
![](https://img.aspxhome.com/file/2023/5/134745_0s.png)
用javascript结合Cookies记录浏览历史
2008-02-12 12:37:00
Bootstrap每天必学之响应式导航、轮播图
2023-08-15 03:29:45
Python 权限控制模块 Casbin
2022-09-21 07:35:46
![](https://img.aspxhome.com/file/2023/5/121135_0s.png)
go中的unsafe包及使用详解
2023-10-13 17:07:27
python操作数据库之sqlite3打开数据库、删除、修改示例
2024-01-26 15:47:01
关于指令重排现象的两个阶段详解
2024-01-05 04:01:04
![](https://img.aspxhome.com/file/2023/9/132439_0s.png)
Bootstrap编写导航栏和登陆框
2023-08-16 19:08:42
Python根据当前日期取去年同星期日期
2021-09-14 15:01:48
python列表的增删改查实例代码
2021-08-11 04:06:51
SQL update 多表关联更新的实现代码
2024-01-22 01:44:06
成功解决ValueError: Supported target types are:('binary', 'multiclass'). Got 'continuous' instead.
2023-01-24 03:59:00
VMware中linux环境下oracle安装图文教程(二)ORACLE 10.2.05版本的升级补丁安装
2023-07-22 11:37:11
![](https://img.aspxhome.com/file/2023/1/64091_0s.jpg)
NodeJs Express框架操作MongoDB数据库执行方法讲解
2024-01-25 08:59:08
JS中setTimeout()的用法详解
2023-06-26 20:55:17
![](https://img.aspxhome.com/file/2023/6/55876_0s.gif)
python中的实例方法、静态方法、类方法、类变量和实例变量浅析
2021-11-06 01:52:14
Navicat for MySQL定时备份数据库及数据恢复详解
2024-01-15 22:49:38
![](https://img.aspxhome.com/file/2023/4/115554_0s.png)
linux安装python修改默认python版本方法
2021-07-19 00:06:41
![](https://img.aspxhome.com/file/2023/0/114600_0s.png)