HTML会自动突出显示打开表中的当前日期

时间:2018-01-19 18:45:51

标签: javascript html css

请注意我们刚刚启动了HTML和CSS,对不起基本问题:)

我们必须为学校项目制作一个关于我们选择的主题或事物的Webside。我选择了我的叔叔金工车间。我想用Openening小时来实现一个表。我搜索了网络,发现我必须使用Javascript,我从未使用过。我尝试了一些东西,但它没有用。

应该获取当天的日期,然后获取工作日ID并添加CSS类以进一步格式化。

function weekday() {
    var d = new Date();

    if (d.getDay() = 0)     {
                        document.getElementById("Sunday").classList.add('OpeningDay');
                        }

    if (d.getDay() = 1)     {
                        document.getElementById("Monday").classList.add('OpeningDay');
                        }

    if (d.getDay() = 2)     {
                        document.getElementById("Tuesday").classList.add('OpeningDay');
                        }

    if (d.getDay() = 3)     {
                        document.getElementById("Wednesday").classList.add('OpeningDay');
                        }

    if (d.getDay() = 4)     {
                        document.getElementById("Thursday").classList.add('OpeningDay');
                        }

    if (d.getDay() = 5)     {
                        document.getElementById("Friday").classList.add('OpeningDay');
                        }

    if (d.getDay() = 6)     {
                        document.getElementById("Saturday").classList.add('OpeningDay');
                        }                   
                    }

我想我可以用CASE Of做到这一点,但正如我所说,我从未使用过Javascript

3 个答案:

答案 0 :(得分:0)

如果条件应该像d.getDay()== 0那样是布尔值。你正在做的是一个赋值。

答案 1 :(得分:0)

.OpeningDay { background-color: yellow; }

<div id="Sunday">
Sunday
</div>
<div id="Monday">
Monday
</div>
<div id="Tuesday">
Tuesday
</div>
<div id="Wednesday">
Wednesday
</div>
<div id="Thursday">
Thursday
</div>
<div id="Friday">
Friday
</div>
<div id="Saturday">
Saturday
</div>

weekday();

function weekday() {
    var d = new Date();

    if (d.getDay() == 0)     {
                        document.getElementById("Sunday").classList.add('OpeningDay');
                        }

    if (d.getDay() == 1)     {
                        document.getElementById("Monday").classList.add('OpeningDay');
                        }

    if (d.getDay() == 2)     {
                        document.getElementById("Tuesday").classList.add('OpeningDay');
                        }

    if (d.getDay() == 3)     {
                        document.getElementById("Wednesday").classList.add('OpeningDay');
                        }

    if (d.getDay() == 4)     {
                        document.getElementById("Thursday").classList.add('OpeningDay');
                        }

    if (d.getDay() == 5)     {
                        document.getElementById("Friday").classList.add('OpeningDay');
                        }

    if (d.getDay() == 6)     {
                        document.getElementById("Saturday").classList.add('OpeningDay');
                        }                   
                    }

小提琴https://jsfiddle.net/hcxnpk5r/

答案 2 :(得分:0)

甚至简单!

&#13;
&#13;
weekday();

function weekday() {
  var d = new Date().getDay();
  document.getElementById("day-"+d).classList.add('opening-day');    
}
&#13;
.opening-day { 
  background-color: yellow; 
}
&#13;
<div id="day-0">
  Sunday
</div>
<div id="day-1">
  Monday
</div>
<div id="day-2">
  Tuesday
</div>
<div id="day-3">
  Wednesday
</div>
<div id="day-4">
  Thursday
</div>
<div id="day-5">
  Friday
</div>
<div id="day-6">
  Saturday
</div>
&#13;
&#13;
&#13;