取决于星期几,JavaScript更改div的CSS属性 [英] JavaScript Change CSS property of div depending on day of the week

查看:81
本文介绍了取决于星期几,JavaScript更改div的CSS属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建网站.我正在显示开放时间.客户要求突出显示星期几.每天都会在黑色方框中显示,而今天的日期则在红色方框中.

I'm in the process of creating a website. I'm displaying the opening hours. And the client has asked for the current day of the week to be highlighted.E.G. every day is displayed in the color black box but today's date is in a red box.

var d = new Date();
var weekday = new Array(7);
weekday[0] =  "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

var n = weekday[d.getDay()];

if (n = 'Thursday') {
    $('coloured-box').css({"color":"green"});
}

以上是我不知道是对的还是对的.

The above is what I have no idea if it's right wrong or nearly there.

推荐答案

最简单的方法是获取当天的日期,然后通过容器中其索引在给定元素上设置类,如下所示:

The simplest way to do this would be to get the current day, then set the class on the given element by its index in its container, like this:

$('.coloured-box span').eq(new Date().getDay()).addClass('today');

.coloured-box span.today { color: #C00; }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="coloured-box">
  <span>Sunday</span>
  <span>Monday</span>
  <span>Tuesday</span>
  <span>Wednesday</span>
  <span>Thursday</span>
  <span>Friday</span>
  <span>Saturday</span>
</div>

这篇关于取决于星期几,JavaScript更改div的CSS属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆