我对javascript很新并且遇到了我的问题,这个地方在不同月份开放不同,所以我希望能够阅读这个月,然后告诉我它是否开放。
<script type="text/javascript">
var time = newDate().getHours();
var opening;
var month = newDate().getMonth();
if (month == 0) {
if (time <= 17) {
opening = "Open";
}
else {
opening = "closed";
}
}
else if (month == 1) {
if (time <=18) {
opening = "Open";
}
else {
opening = "closed";
}
}
else if (month = 3) {
if (time <=19) {
opening = "Open";
}
else {
opening = "closed";
}
}
else if (month = 4) {
if (time <=19) {
opening = "Open";
}
else {
opening = "closed";
}
}
else if (month = 5) {
if (time <=19) {
opening = "Open";
}
else {
opening = "closed";
}
}
else if (month = 6 ) {
if (time <=21) {
opening = "Open";
}
else {
opening = "closed";
}
}
else if (month = 7) {
if (time <=19) {
opening = "Open";
}
else {
opening = "closed";
}
}
else if (month = 8) {
if (time <=19) {
opening = "Open";
}
else {
opening = "closed";
}
}
else if (month = 9) {
if (time <=19) {
opening = "Open";
}
else {
opening = "closed";
}
}
else if (month = 10) {
if (time <=19) {
opening = "Open";
}
else {
opening = "closed";
}
}
else (month = 11) {
if (time <=19) {
opening = "Open";
}
else {
opening = "closed";
}
}
document.getElementById("demo").innerHTML = opening;
</script>
我查了一些例子,但似乎无法得到它
答案 0 :(得分:0)
将所有值从if(month = 3)
更改为if(month == 3)
,将3
更改为11
。它会工作。你错过了month == 2
的条件。
首选swich()
用于此类案件。它的mich更干净
答案 1 :(得分:0)
一些问题:
newDate()
应为new Date()
==
进行比较而不是=
,您在大多数地方都错过了它:注意else if (month = 3) {
与else if (month == 3) {
else
应该是else if
,因为您已在其中设置条件:else if (month == 11) {