html部分:
<div id="ll">
<div id="box">
<div id="aa">
<div id="bb">星期二</div>
<div id="cc">9</div>
</div>
<div id="dd">
<div id="ee">2023-05-09</div>
</div>
<div id="ff">
<div id="gg">
<div class="a1">三月二十</div>
<div class="a2">癸卯年 兔</div>
<div class="a3">丁巳月 丁卯日</div>
</div>
<div id="hh">
<div class="a4">宜:订盟 纳采 祭祀 动土 破土 交易 立券</div>
<div class="a5"> 忌:嫁娶 安葬 </div>
</div>
</div>
<div>
<input type="text" id="bwl">
<button id="bwl-1">保存</button>
<button id="bwl-2">清除</button>
</div>
</div>
<select id="yun" οnchange="getRun();jyq();sum()"></select>
<select id="yun1" οnchange="jyq()"></select>
<button id="nav6" οnclick="uu()">上一月</button>
<button id="nav7" οnclick="zz()">下一月</button>
<button id="fan" οnclick="oo()">返回今天</button>
<div class="e1">
<table class="e2">
<tr>
<td class="e3">一</td>
<td class="e3">二</td>
<td class="e3">三</td>
<td class="e3">四</td>
<td class="e3">五</td>
<td class="e3">六</td>
<td class="e3">日</td>
</tr>
</table>
<div id="date">
</div>
</div>
</div>
</div>
css部分:
* {
border: 0;
border-collapse: collapse;
border-spacing: 0;
list-style: none;
margin: 0;
padding: 0;
}
#ll{
width: 610px;
height: 560px;
background-image: url(16154396841752805.gif);
background-repeat: no-repeat;
background-size:610px 559px;
border: 1px solid rgb(240, 215, 215);
}
#box {
width: 574px;
height: 88px;
color: #fff;
/* background: linear-gradient(180deg,#0064d9,#11428c); */
border-radius: 6px 6px 0 0;
padding: 0px 18px;
display: flex;
min-height: 88px;
align-items: center;
}
#aa {
width: 64px;
height: 64px;
color: #111;
display: flex;
flex-direction: column;
margin-right: 20px;
border-radius: 8px;
background: rgb(248, 240, 240);
align-items: center;
justify-content: center;
position: relative;
}
#bb {
width: 48px;
height: 20.8px;
color: rgb(8, 72, 128);
font-size: 12px;
align-items: center;
padding: 3px 0px 0px 11px;
/* border: 1px solid yellow; */
}
#cc {
width: 17.8px;
height: 25px;
font-size: 25px;
/* border: 1px solid rgb(9, 194, 207); */
}
#dd {
width: 170px;
height: 32px;
display: flex;
flex-direction: column;
line-height: 20px;
justify-content: center;
min-width: 170px;
/* border: 1px solid rgb(64, 247, 9); */
}
#ee {
line-height: 32px;
font-size: 24px;
font-weight: 600;
}
#ff {
width: 261px;
height: 60px;
border-left: 1px solid rgba(255, 255, 255, .3);
padding-left: 16px;
display: flex;
flex-direction: column;
justify-content: center;
}
#gg {
width: 244px;
height: 20px;
display: flex;
line-height: 20px;
/* border: 1px solid rgb(252, 3, 190); */
}
.a1 {
width: 86px;
height: 20px;
font-size: 14px;
padding: 0px 4px 0px 0px;
/* border: 1px solid rgb(2, 226, 32); */
}
.a2 {
width: 70px;
height: 20px;
font-size: 14px;
/* border: 1px solid rgb(0, 231, 212); */
}
.a3 {
width: 101px;
height: 20px;
font-size: 14px;
/* border: 1px solid rgb(231, 255, 17); */
}
#hh {
width: 244px;
height: 40px;
}
.a4 {
width: 243px;
height: 20px;
font-size: 14px;
/* border: 1px solid red; */
}
.a5 {
width: 101px;
height: 20px;
font-size: 14px;
/* border: 1px solid blue; */
}
#date {
border: 1px solid rgba(236, 236, 236, 0.733);
width: 400px;
}
#yun {
border: 1px solid gray;
width: 80px;
height: 25px;
text-align: center;
border-radius: 10px;
}
#yun1 {
border: 1px solid gray;
width: 60px;
height: 25px;
text-align: center;
border-radius: 10px;
}
#nav6{
border: 1px solid gray;
width: 60px;
height: 25px;
text-align: center;
border-radius: 10px;
}
#nav7{
border: 1px solid gray;
width: 60px;
height: 25px;
text-align: center;
border-radius: 10px;
}
#fan {
width: 80px;
height: 22px;
border-radius: 10px;
border: 1px solid rgba(221, 221, 230, 0.909);
color: rgba(6, 6, 237, 0.909);
background-color: white;
}
.e1 {
width: 608px;
height:445px;
border: 1px solid rgba(255, 249, 249, 0.863);
}
.e2 {
width: 608px;
height: 36px;
display: flex;
border: 1px solid rgb(243, 243, 237);
}
.e3 {
padding: 4px;
width: 80px;
height: 35px;
text-align: center;
border: 1px solid rgba(209, 209, 209, 0.438);
}
.tdx {
border: 1px solid transparent;
height: 77px;
width: 88px;
font-size: 20px;
font-weight: 600;
text-align: center;
}
.tdx:hover {
border: 2px solid #b9b4b4;
}
#date{
width: 606px;
height: 406px;
/* border: 1px solid rgb(248, 242, 242); */
}
#bwl{
width: 80px;
height: 80px;
border: 1px solid red;
margin: 16px 0px 0px 2px;
}
#bwl-1{
width: 26px;
height: 25px;
font-size: 10px;
position: relative;
right: -56px;
top: 10px;
border: 1px solid blue;
}
#bwl-2{
width: 26px;
height: 25px;
font-size: 10px;
position: relative;
right: 10px;
top: 10px;
border: 1px solid pink;
}
Java script部分:
let newday = new Date(); //设定一个全局变量
let nian = newday.getFullYear(); //返回年份
let yue = newday.getMonth(); //返回月份
let day = newday.getDate();
let months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; //定义数组中月份的天数
let Day = document.querySelector("#Day");
let yun = document.querySelector("#yun");
let yun1 = document.querySelector("#yun1");
//循环出年、月
for (var i = 1970; i < 2050; i++) {
var newyun = document.createElement("option")
newyun.text = i + "年";
newyun.value = i;
yun.add(newyun);
}
yun.value = newday.getFullYear();
for (var i = 1; i <= 12; i++) {
var newyun1 = document.createElement("option")
newyun1.text = i + "月";
newyun1.value = i;
yun1.add(newyun1);
}
yun1.value = newday.getMonth() + 1;
//判断平年还是闰年 //这个表达式不是判断year是否“是闰年”,而是判断year是否“不是闰年”。
function getRun() {
const year = document.getElementById('yun').value;
if ((year % 4 != 0) || (year % 100 == 0 && year % 400 != 0)) {
months[1] = 29;
} else {
months[1] = 28;
}
}
getRun();
let shang = document.getElementById('nav6');
let xia = document.getElementById('nav7');
let hui = document.getElementById('fan');
function uu() {
yun1.value -= 1;
if (yun1.value == '') {
yun1.value = 12;
yun.value -= 1;
}
jyq();
}
function zz() {
yun1.value = yun1.value * 1 + 1;
if (yun1.value == '') {
yun1.value = 1;
yun.value = yun.value * 1 + 1;
}
jyq();
}
function oo() {
yun.value = nian;
yun1.value = yue + 1;
jyq();
}
//表格
function jyq() {
const year = yun.value;
const month = yun1.value;
const monthNum = months[month - 1];//monthNum:月数
const week = new Date(year, month - 1, 1).getDay();
const box1 = document.querySelector("#date");
box1.innerHTML = "";
var num = 1;
for (let i = 0; i < 6; i++) {
let tr = document.createElement("tr");
for (let j = 0; j < 7; j++) {
let td = document.createElement('td');
if (!(j < week - 1 && i == 0)) {
if (num <= monthNum) {
td.innerHTML = num++;
td.classList.add("day", "tdx");
}
}
tr.appendChild(td);
}
box1.appendChild(tr);
}
//点击月份中的几号跳转
let yi = document.querySelectorAll("td.day");
for (let i = 0; i < yi.length; i++) {
yi[i].onclick = function () {
day = this.innerText * 1;
sum();
bwl()
}
}
}
jyq();
bwl()
for (let i = 0; i < yun.length; i++) {
yun[i].onchange = function () {
nain = this.innerText * 1;
}
// for (let j = 0; j < yun1.length; j++) {
// yun1[j].onchange = function () {
// yue = this.innerText * 1;
// }
// }
}
function sum() {
let tempdate = calendar.solar2lunar(yun.value, yun1.value, day);
console.log(tempdate);
document.querySelector("#bb").innerText = tempdate.ncWeek;
document.querySelector("#cc").innerText = day;
document.querySelector("#ee").innerText = `${yun.value}-${yun1.value}-${day}`;
document.querySelector(".a1").innerText = tempdate.IMonthCn + tempdate.IDayCn;
document.querySelector(".a2").innerText = tempdate.gzYear + tempdate.Animal;
document.querySelector(".a3").innerText = tempdate.gzMonth + tempdate.gzDay;
}
function getCookie() {
const strCookie = document.cookie;
const obj = {};
if (strCookie != "") {
const cookitem = strCookie.split("; ")
console.log(cookitem);
for (let i = 0; i < cookitem.length; i++) {
const arr = cookitem[i].split("=");
obj[arr[0]] = arr[1];
}
}
return obj;
}
getCookie();
document.getElementById("bwl-1").onclick = function baocuncookie() {
const bwl = document.getElementById("bwl");
const years = document.getElementById("ee");
if (bwl.value == "") {
const obj = getCookie();
if (!(obj[years.innerHTML])) {
console.log(obj[years.innerHTML]);
return
}
}
document.cookie = `${years.innerText}=${bwl.value};max-age=${60*10}`;
}
document.getElementById("bwl-2").οnclick=function quchucookie(){
const bwl = document.getElementById("bwl");
document.cookie=bwl.value="";
}
function bwl() {
const bwl = document.getElementById("bwl");
bwl.value = "";
const years = document.getElementById("ee");
const obj = getCookie();
if (obj[years.innerHTML]) {
bwl.value = obj[years.innerHTML];
}
}