语法错误:期望的表达式,得到'<='

Syntax Error: expected expression, got '<='

本文关键字:得到 错误 期望 表达式 语法      更新时间:2023-09-26

试图根据嵌套if/then语句中的两个变量更改背景图像。第17行出现语法错误(语法错误:expected expression, got '<=')

我读了一些类似主题的帖子,但它们似乎是不相关的,或者我只是不明白在说什么…这只是普通的JS,没有任何。JS被引用。应该有吗?没有jquery等

在我的幻想中,时间将被传递到这个页面,并导致一个时间适当的图像(从一组三)随机显示(例如,夜间访问者将看到三张夜间图像中的一张)在背景中。如果有人有一个比我在下面拼凑的更好的系统,我将包含这个。

randomLocale = Math.floor((Math.random() * 10) + 1);
if (randomLocale <= 3 ) {
    if (time >= 730 && <= 1830) {
        document.body.style.backgroundImage = "url(../images/cloister_daytime_01.png)";
    } else if (time >= 2100 && <=530) {
        document.body.style.backgroundImage = "url(../images/cloister_night_01.png)";
    } else {
        document.body.style.backgroundImage = "url(../images/cloister_dusk_01.png)";
    }
} else if (randomLocale >= 7) {
    if (time >= 730 && <= 1830) {
        document.body.style.backgroundImage = "url(../images/cloister_daytime_02.png)";
    } else if (time >= 2100 && <=530) {
        document.body.style.backgroundImage = "url(../images/cloister_night_02.png)";
    } else {
        document.body.style.backgroundImage = "url(../images/cloister_dusk_02.png)";
    }
} else {
    if (time >= 730 && <= 1830) {
        document.body.style.backgroundImage = "url(../images/cloister_daytime_03.png)";
    } else if (time >= 2100 && <=530) {
        document.body.style.backgroundImage = "url(../images/cloister_night_03.png)";
    } else {
        document.body.style.backgroundImage = "url(../images/cloister_dusk_03.png)";
    }
}

This

else if (time >= 2100 && <=530) {

在与号和小于号之间缺少一个变量。

如果你想检查一个变量是否在两个值之间,你需要这样做:

if (x >= 1 && x <= 15)

这一行:if (time >= 730 && <= 1830) {

应该是if (time >= 730 && time <= 1830) {