我试图让这个工作在每月的每个数字日。它没有||。任何想法如何使它与||?
一起使用<script>
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = (month) + "-" + (day) + "-" + now.getFullYear();
var d = new Date();
var n = d.getDate();
if ((day == 13) ||
(day == 16) ||
(day == 19) ||
(day == 22)) {
document.getElementById("shift").innerHTML="A SHIFT" + " " + today;
}
else if ((day == 14) ||
(day == 17) ||
(day == 20) ||
(day == 23)) {
document.getElementById("shift").innerHTML="B SHIFT" + " " + today;
}
else ((day == 15) ||
(day == 18) ||
(day == 21) ||
(day == 24)) {
document.getElementById("shift").innerHTML="C SHIFT" + " " + today;
}
</script>
答案 0 :(得分:0)
您的代码不会产生任何输出,因为存在语法错误。修复似乎解决了您的问题 - jsFiddle。
您无法通过条件跟踪else
。只有在您说else if
时才有效。
else ((day == 15) ||
(day == 18) ||
(day == 21) ||
(day == 24)) {
生成Uncaught SyntaxError: Unexpected token {
或类似内容(取决于您的浏览器)。
正确的代码:
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = (month) + "-" + (day) + "-" + now.getFullYear();
var d = new Date();
var n = d.getDate();
if ((day == 13) || (day == 16) || (day == 19) || (day == 22)) {
document.getElementById("shift").innerHTML = "A SHIFT " + today;
} else if ((day == 14) || (day == 17) || (day == 20) || (day == 23)) {
document.getElementById("shift").innerHTML = "B SHIFT " + today;
} else if ((day == 15) || (day == 18) || (day == 21) || (day == 24)) {
document.getElementById("shift").innerHTML = "C SHIFT " + today;
}
答案 1 :(得分:0)
或者,您可以重构整个解决方案并使目标更具可读性
<script language="javascript" type="text/javascript">
// Get todays date and split it into dd, yyyy, mm components
var now = new Date();
var yyyy = now.getFullYear().toString();
var mm = (now.getMonth()+1).toString();
var dd = now.getDate().toString();
// Build padded_date in the format mm-dd-yyyy
var padded_date = (mm[1]?mm:"0"+mm[0]) + '-' + (dd[1]?dd:"0"+dd[0]) + '-' + yyyy;
alert(padded_date);
// Determine the only variable that changes in your latter code
var shift_alphabet;
switch (parseInt(dd)) {
case 13:
case 16:
case 19:
case 22: shift_alphabet = 'A';
break;
case 14:
case 17:
case 20:
case 23: shift_alphabet = 'B';
break;
case 15:
case 18:
case 21:
case 24: shift_alphabet = 'C';
break;
default: shift_alphabet = 'A' // Assumption
break;
}
// Using alerts because I don't have the HTML stuff
alert(shift_alphabet);
// Append shift alphabet to your output
// document.getElementById("shift").innerHTML= shift_alphabet + " SHIFT" + " " + padded_date;