如果上课日期是今天,如何添加课程? [英] How to add class if content date is today?

查看:41
本文介绍了如果上课日期是今天,如何添加课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上有一个日程表日历,并希望通过使用CSS添加红色边框(仅作为示例)使当日与其他日期脱颖而出.我可能需要使用JavaScript来确定内容是否为当天.我一直在尝试执行此操作,但是大多数指南都在使用data-date,因此这让我感到困惑.

I have a schedule calendar om my website, and want to make the current day stand out from the other days by using css to add a red border (just a example). I would probably need to use JavaScript to determine if the content is the current day. I have been trying to do this, but mostly guides out there are using data-date, so that makes me stuck.

html输出看起来像这样

The html output looks like this

<span class="date-display-single" property="dc:date" datatype="xsd:dateTime" content="2019-02-28T11:30:00+01:00">28.02</span>

如果content ="2019-02-28T11:30:00 + 01:00"是今天,我该如何添加课程.

How so i add a class if the content="2019-02-28T11:30:00+01:00", is today.

谢谢.

推荐答案

您可以使用 DOM 来获取html元素的值( innerHTML ).然后,最后,您可以使用 .className 添加您想要对该元素进行的类.

You can use Date to find the current day and month, which is a very valuable tool. The if statement checks to make sure the date and month are in mm/dd format. Then you can use DOM to get the value of your html element (innerHTML). Then finally, you can use .className to add the class you wanted to the element.

var d = new Date();
var month = d.getMonth() + 1; //.getMonth() is 0-11
var day = d.getDate();

if (day < 10) {
  day = '0' + dd;
}

if (month < 10) {
  month = '0' + month;
}

if (document.getElementById("day").innerHTML == `${day}.${month}`) {
  document.getElementById("day").className += " today";
}

.today {
  border: 2px solid red;
}

<span class="date-display-single" id="day" property="dc:date" datatype="xsd:dateTime" content="2019-02-28T11:30:00+01:00">23.02</span>

这篇关于如果上课日期是今天,如何添加课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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