如何使用按钮来更改JavaScript代码的样式?



在要求我们使用变量创建 12 月份的日历的前几天,当天将以颜色显示。工作日将采用不同的颜色。但是,我正在努力创建按钮,该按钮将在当天后的第二天更改,以具有不同的样式,例如使用CSS更改字体或颜色。以及一个按钮,该按钮将更改当天的一周,使其具有与当前第二天不同的样式。 这是代码:

<!DOCTYPE html>
<html lang="en-us">
<head>
<title>22plazaa-Unit 3 Day 7</title>
<meta charset="UTF-8">
<style>
* {font-family: "Times New Roman", sans-serif;}
table, th, td {
border: 3px solid pink;
border-collapse: collapse;
}
table {
margin:auto;
}
td {
width: 10%;
padding: 20px;
font-size: 24pt;
text-align: center;
}
h1 {text-align: center}
#empty {background-color:azure}
</style>
</head>
<body>
<h1>Unit 3 Day 7</h1>
<table>
<tr>
<th id="sun">Sunday</th>
<th id="mon">Monday</th>
<th id="tue">Tuesday</th>
<th id="wed">Wednesday</th>
<th id="thu">Thursday</th>
<th id="fri">Friday</th>
<th id="sat">Saturday</th>
</tr>
<tr>
<td id="d1">1</td>
<td id="d2">2</td>
<td id="d3">3</td>
<td id="d4">4</td>
<td id="d5">5</td>
<td id="d6">6</td>
<td id="d7">7</td>
</tr>
<tr>
<td id="d8">8</td>
<td id="d9">9</td>
<td id="d10">10</td>
<td id="d11">11</td>
<td id="d12">12</td>
<td id="d13">13</td>
<td id="d14">14</td>
</tr>
<tr>
<td id="d15">15</td>
<td id="d16">16</td>
<td id="d17">17</td>
<td id="d18">18</td>
<td id="d19">19</td>
<td id="d20">20</td>
<td id="d21">21</td>
</tr>
<tr>
<td id="d22">22</td>
<td id="d23">23</td>
<td id="d24">24</td>
<td id="d25">25</td>
<td id="d26">26</td>
<td id="d27">27</td>
<td id="d28">28</td>
</tr>
<tr>
<td id="d29">29</td>
<td id="d30">30</td>
<td id="d31">31</td>
<td id="end" colspan="4"></td>
</tr>
</table>
<script>
var today = new Date(); 
switch (today.getDay()) {
case 0:
document.getElementById("sun").style.backgroundColor = "lightblue";
break;
case 1:
document.getElementById("mon").style.backgroundColor = "lightblue";
break;
case 2:
document.getElementById("tue").style.backgroundColor = "lightblue";
break;
case 3:
document.getElementById("wed").style.backgroundColor = "lightblue";
break;
case 4:
document.getElementById("thu").style.backgroundColor = "lightblue";
break;
case 5:
document.getElementById("fri").style.backgroundColor = "lightblue";
break;
case 6:
document.getElementById("sat").style.backgroundColor = "lightblue";
break;
}
var date = document.getElementById("d" + today.getDate());
date.style.color = "white";
date.style.backgroundColor = "salmon";

</script>
</body>
</html>

答案是每次调用 click 事件时只需更改样式 我制作了一个显示运行的代码片段。

我不确定你所说的不同风格试图详细说明是什么意思。

var nday = document.querySelector('.nday');
var nweek = document.querySelector('.nweek');
nday.addEventListener('click', ()=>{
document.getElementById(Day[today.getDay()]).style.backgroundColor = "white"
let date = document.getElementById("d" + today.getDate());
date.style.color = "black";
date.style.backgroundColor = "white";
today.setDate(today.getDate() + 1)
document.getElementById(Day[today.getDay()]).style.backgroundColor = "lightBlue"
date = document.getElementById("d" + today.getDate());
date.style.color = "white";
date.style.backgroundColor = "salmon";
})
nweek.addEventListener('click', ()=>{
document.getElementById(Day[today.getDay()]).style.backgroundColor = "white"
let date = document.getElementById("d" + today.getDate());
date.style.color = "black";
date.style.backgroundColor = "white";
today.setDate(today.getDate() + 7)
document.getElementById(Day[today.getDay()]).style.backgroundColor = "lightBlue"
date = document.getElementById("d" + today.getDate());
date.style.color = "white";
date.style.backgroundColor = "salmon";
})
var Day = ["sun", "mon", "tue","wed", "thu", "fri", "sat"]
var today = new Date();
document.getElementById(Day[today.getDay()]).style.backgroundColor = "lightBlue"
var date = document.getElementById("d" + today.getDate());
date.style.color = "white";
date.style.backgroundColor = "salmon";
* {font-family: "Times New Roman", sans-serif;}
table, th, td {
border: 3px solid pink;
border-collapse: collapse;
}
table {
margin:auto;
}
td {
width: 10%;
padding: 20px;
font-size: 24pt;
text-align: center;
}
h1 {text-align: center}
#empty {background-color:azure}
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>22plazaa-Unit 3 Day 7</title>
<meta charset="UTF-8">
</head>
<body>
<h1>Unit 3 Day 7</h1>
<button class = "nday">Next Day</button>
<button class = "nweek">Next Week</button>
<table>
<tr>
<th id="sun">Sunday</th>
<th id="mon">Monday</th>
<th id="tue">Tuesday</th>
<th id="wed">Wednesday</th>
<th id="thu">Thursday</th>
<th id="fri">Friday</th>
<th id="sat">Saturday</th>
</tr>
<tr>
<td id="d1">1</td>
<td id="d2">2</td>
<td id="d3">3</td>
<td id="d4">4</td>
<td id="d5">5</td>
<td id="d6">6</td>
<td id="d7">7</td>
</tr>
<tr>
<td id="d8">8</td>
<td id="d9">9</td>
<td id="d10">10</td>
<td id="d11">11</td>
<td id="d12">12</td>
<td id="d13">13</td>
<td id="d14">14</td>
</tr>
<tr>
<td id="d15">15</td>
<td id="d16">16</td>
<td id="d17">17</td>
<td id="d18">18</td>
<td id="d19">19</td>
<td id="d20">20</td>
<td id="d21">21</td>
</tr>
<tr>
<td id="d22">22</td>
<td id="d23">23</td>
<td id="d24">24</td>
<td id="d25">25</td>
<td id="d26">26</td>
<td id="d27">27</td>
<td id="d28">28</td>
</tr>
<tr>
<td id="d29">29</td>
<td id="d30">30</td>
<td id="d31">31</td>
<td id="end" colspan="4"></td>
</tr>
</table>
</body>
</html>

相关内容

  • 没有找到相关文章

最新更新