gpt4 book ai didi

javascript - 我正在努力争取那一天 - Javascript

转载 作者:行者123 更新时间:2023-11-30 11:13:59 24 4
gpt4 key购买 nike

我有一个脚本可以找到那天、日期和年份。但是两天它应该在当天下面说“我关闭”......这两天是“Søndag”和“Onsdag”我试图做一个 if 语句,但它似乎不起作用:(

HTML

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>JS - Testing</title>
</head>
<body>
<b id="calendar-day"></b> -
<b id="calendar-date"></b>
<b id="calendar-month-year"></b>
<p></p>
</body>
<script src="main.js"></script>
</html>

JS

//function that gets the day and date
function calendar() {
var day = ['Søndag', 'Mandag','Tirsdag','Onsdag','Torsdag','Fredag','Lørdag'];
var month = ['Januar','Febuar','Marts','April','Maj','Juni','Juli','August','September','Oktober','November','December'];
var d = new Date();
setText('calendar-day', day[d.getDay()]);
setText('calendar-date', d.getDate());
setText('calendar-month-year', month[d.getMonth()]+' '+(1900+d.getYear()));
checkDay();
};

//function that sees if it's closing day
var paragraph = document.querySelector('p');

function checkDay() {
if (day == 'Onsdag') {
paragraph.innerText = 'I am closed';
}
}

function setText(id, val){
if(val < 10){
val = '0' + val; //add leading 0 if val < 10
}
document.getElementById(id).innerHTML = val;
};

window.onload = calendar;

最佳答案

可能您正在尝试这样做。您不能直接将 day 访问到 checkDay 函数中,而是需要将其作为参数传递,然后再传递。其次,你不能直接从 day 中提取一个值,因为它是一个数组,不是整数或字符串,所以你需要遍历数组才能找到正确的值

function calendar() {
var day = ['Søndag', 'Mandag','Tirsdag','Onsdag','Torsdag','Fredag','Lørdag'];
var month = ['Januar','Febuar','Marts','April','Maj','Juni','Juli','August','September','Oktober','November','December'];
var d = new Date();
var today = day[d.getDay()];
setText('calendar-day', today);
setText('calendar-date', d.getDate());
setText('calendar-month-year', month[d.getMonth()]+' '+(1900+d.getYear()));
checkDay(today);
};

//function that sees if it's closing day
var paragraph = document.querySelector('p');

function checkDay(day) {
if (day == 'Onsdag' || day == 'Søndag') {
paragraph.innerText = 'I am closed';
} else paragraph.innerText = 'I am opened';

}

function setText(id, val){
if(val < 10){
val = '0' + val; //add leading 0 if val < 10
}
document.getElementById(id).innerHTML = val;
};

window.onload = calendar;
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>JS - Testing</title>
</head>
<body>
<b id="calendar-day"></b> -
<b id="calendar-date"></b>
<b id="calendar-month-year"></b>
<p></p>
</body>
<script src="main.js"></script>
</html>

关于javascript - 我正在努力争取那一天 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52402933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com