[英]Javascript If statement HTML
I have the following code to generate the current date in a specific format.我有以下代码以特定格式生成当前日期。 I want to have an if statement that will change the back ground color and the font color depending if the specific day is on a Sunday, Saturday, or a Week day.
我想要一个 if 语句来更改背景颜色和字体颜色,具体取决于特定日期是星期日、星期六还是工作日。
var date = new Date();
var dayMonthYear = date.toLocaleString('en-US', {month:'long',day:'numeric',year:'numeric'}).replaceAll(", ", "-");
var weekday = date.toLocaleString('en-US', { weekday: 'long' })
document.write(dayMonthYear + ' [' + weekday + ']');
I thought I would just create a general if statement that looks at the weekday variable but it doesn't seem to work correctly like I would think.我以为我会创建一个通用的 if 语句来查看工作日变量,但它似乎不像我想的那样正常工作。
if (weekday = 'Sunday') {
background-color:aqua;
color: blue;
}
Also, any ideas on how to get a - between the Month and Day number?此外,关于如何获得 - 在月份和日期之间的数字有什么想法吗?
Example: August-15-2021 My current code generates August 15-2021示例:August-15-2021 我当前的代码生成 August 15-2021
A good approach to this would be to make your preferred stylings as different classes then add that class to your HTML using JavaScript.一个好的方法是将您喜欢的样式设置为不同的类,然后使用 JavaScript 将 class 添加到您的 HTML。
That way you can avoid the if statements altogether这样你就可以完全避免 if 语句
I have made an applyStyling
function that takes the weekday
variable as an argument我制作了一个
applyStyling
function 将weekday
变量作为参数
function applyStyling(day){ let body = document.querySelector("body"); body.classList.add(day.toLowerCase()); } var date = new Date(); var dayMonthYear = date.toLocaleString('en-US', {month:'long',day:'numeric',year:'numeric'}).replaceAll(", ", "-"); var weekday = date.toLocaleString('en-US', { weekday: 'long' }) document.write(dayMonthYear + ' [' + weekday + ']'); applyStyling(weekday);
.sunday { background-color:aqua; color: blue; }.monday { background-color:lightcoral; color: brown; }
Single =
is assigning value to the variable, while double ==
or triple ===
is a comparison between two values. Single
=
是给变量赋值,而 double ==
或 triple ===
是两个值之间的比较。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
Once you corrected the expression, the actual color change of a HTML element can be done via inline style:更正表达式后,可以通过内联样式完成 HTML 元素的实际颜色更改:
element.style.backgroundColor = "aqua";
or better yet by changing/adding css class of the element:或者更好的是通过更改/添加元素的 css class:
element.classList.add("aqua");
As for adding -
you can use this regex / (\d)/
至于添加
-
你可以使用这个正则表达式/ (\d)/
var date = new Date(); var dayMonthYear = date.toLocaleString('en-US', {month:'long',day:'numeric',year:'numeric'}).replaceAll(", ", "-"); var weekday = date.toLocaleString('en-US', { weekday: 'long' }) console.log(dayMonthYear.replace(/ (\d)/, '-$1') + ' [' + weekday + ']'); if (weekday == 'Sunday') { document.body.style.backgroundColor = "aqua"; document.body.style.color = "blue"; } document.body.classList.add("myclass");
body.myclass { font-size: 10em; }
test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.