简体   繁体   中英

How to use buttons to change style of javascript code?

A few days before we were asked to create a calender for the month of December and using variables, the current day would be shown witha color. The weekday would be in a different color. However I am struggling with creating button that will change the day after the current day to have a different style as in changing the font or color using CSS. As well as a button that will change the week of the current day to have a different style than the day after the current one. Here is the code:

<!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>

The answer is to simply change styles every time you call the click event I made a code snippet showing the run.

I am not sure of what do you mean by different styles try to elaborate.

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM