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

时间:2015-10-12 02:27:37

标签: javascript if-statement syntax background-image

尝试在嵌套的if / then语句中基于两个变量更改背景图像。第17行出现语法错误(语法错误:期望表达式,得到'< =')

我阅读了一些关于类似主题的帖子,但它们似乎无关,或者我只是不理解所说的内容......这只是普通的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)";
    }
}

2 个答案:

答案 0 :(得分:-1)

这个

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

缺少&符号与小于号之间的变量。

如果要检查变量是否在两个值之间,则需要执行以下操作:

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

答案 1 :(得分:-1)

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

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