原生JavaScript写个表格版的日历

如图:

简单、易懂、写着玩儿的……

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>

<body>
<div id="myDate"></div>
<script>
(function(id, options){
	this._date = new Date()
	this.dateObj = {
		_year: this._date.getFullYear(),
		_month: this._date.getMonth() + 1,
		_date: this._date.getDate(),
		_hour: this._date.getHours(),
		_minutes: this._date.getMinutes(),
		_seconds: this._date.getSeconds(),
	}
	this.config = {
		width: 			options.width ? options.width : 320,
		height: 		options.height ? options.height : 240,
		sundayColor: 	options.sundayColor ? options.sundayColor : '#F7A597',
		SaterdayColor: 	options.SaterdayColor ? options.SaterdayColor : '#D0F7CC',
		curColor: 		options.curColor ? options.curColor : '#f00',
	}
	// 本月多少天
	this.calcMonthDays = function(){
		return new Date(this.dateObj._year, this.dateObj._month, 0).getDate()
	}
	// 1号星期几
	this.calcDay1Week = function(){
		return new Date(this.dateObj._year + '-' + this.dateObj._month + '-' + 1).getDay()
	}
	// 生成日历表头, 周日到周六
	this.createTableTitle = function(){
		var week = ['日', '一', '二', '三', '四', '五', '六'], nullTd = [], i
		for(i=0; i<week.length; i++){
			nullTd.push('<th>' + week[i] + '</th>')
		}
		return nullTd
	}
	// 生成空表格
	this.createNullTd = function(){
		var nullTd = this.createTableTitle(), i
		for(i=0; i<this.calcDay1Week(); i++){
			nullTd.push('<td></td>')
		}
		return nullTd
	}
	// 生成日期表体, 1号到月底
	this.createTableBodyTr = function(){
		var nullTd = this.createNullTd(), totalDays = this.calcMonthDays() + this.calcDay1Week(), i
		for(i=1; i<=this.calcMonthDays(); i++){
			if (this.dateObj._date == i){
				nullTd.push('<td style="color:' + this.config.curColor + '">' + i + '</td>')
			}else{
				nullTd.push('<td>' + i + '</td>')
			}
		}
		console.log(totalDays)
		if (totalDays% 7 != 0){
			for(var i=0; i<(7 - totalDays % 7); i++){
				nullTd.push('<td></td>')
			}
		}
		return nullTd
	}
	// 渲染表格
	this.render = function(id){
		var myDate = document.getElementById(id), 
			nullTd = this.createTableBodyTr()
		var table, tableBegin = '<table style="width:' + this.config.width + 'px;height:' + this.config.height +'px;text-align:center">', tableEnd = '</table>', tr = '<tr>'
			for(var i=0; i<nullTd.length; i++){
				if (i%7 == 0 && i>1){
					tr += '</tr><tr>'
				}
				tr += nullTd[i]
			}
			tr += '</tr>'
			table = tableBegin + tr + tableEnd
			myDate.innerHTML = table
	}
	this.render(id)
	console.log(options)
})('myDate', {width:320, height:240})
</script>
</body>
</html>

非常简单,也易于修改,有兴趣的可以加上上一个月,下一个月等等

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东成2022

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值