我有一个脚本,可以查找当天,日期和年份。 但是两天后,应该在该天的下方说“我关闭了”。 那两天是“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;
答案 0 :(得分:2)
可能您正在尝试执行此操作。您不能直接访问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>
答案 1 :(得分:2)
开发人员中最珍贵的品质是懒惰。不要在那儿乱糟糟:-)使用Date对象来操纵日期,如今,要操纵DOM确实没有理由不使用Vue。
要获取丹麦的工作日,请使用myDate.toLocaleDateString('da-DK',{weekday:'long'})
。月名称也是如此。文档是here。
要确定周末是否有一天,请使用(myDate.getDay() === 6) || (myDate.getDay() === 0);
这是您的示例,具有13行脚本和6行标记。
var vm = new Vue({
el : "#vueRoot",
data : { myDate : new Date() },
computed : {
formattedDate(){
return this.myDate.toLocaleDateString(
'da-DK',
{weekday:'long', day:'numeric', month:'long', year:'numeric'}
);
},
isOpen(){return !(this.myDate.getDay() === 3 || this.myDate.getDay() === 0)}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="vueRoot">
<b>{{formattedDate}}</b>
<br>
{{isOpen?'I am OPEN':'I am CLOSED'}}
</div>
答案 2 :(得分:-1)
我已经过了一天,进入checkDay:
//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()]);
desiredDay = day[d.getDay()];
setText('calendar-date', d.getDate());
setText('calendar-month-year', month[d.getMonth()]+' '+(1900+d.getYear()));
checkDay(desiredDay);
};
//function that sees if it's closing day
var paragraph = document.querySelector('p');
function checkDay(day) {
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;
<!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>
day在checkDay函数中未定义