如何更改此 JavaScript 以设置背景颜色

how can I change this JavaScript to set the background colour

所以我得到了这段代码,其中 returns 您输入的位置的天气。

与其只返回一个图标,我希望它也能改变背景的颜色。

我只是无法理解这一切。

function getLocation(){

    var location = document.getElementById("location").value;

    location = location.replace(" ", "%20");

    if (location == ""){

        document.getElementById("location").classList.add("error");

    }
    else {

        document.getElementById("location").classList.remove("error");
        getWeather(location);

    }
}


function getWeather(location){

    var ajax = new XMLHttpRequest();

    var json;


    var apiKEY = "3521a940efd69dc5b6f3dd982d18c618";

    var url = "http://api.openweathermap.org/data/2.5/weather?q=" + location + " ,uk&appid=" + apiKEY;


    ajax.open("GET", url, true);


    ajax.send();


    ajax.onreadystatechange = function(){


        if (ajax.readyState == 4 && ajax.status == 200){

            json = JSON.parse(ajax.responseText);


            document.getElementById("locationForm").style.display = "none";
            document.getElementById("weather").style.display = "block";

            if (json != undefined){

                var weather = json.weather[0].main
                setIconAndDescription(weather, location)

            }
            else {

                description = "Oops, I couldn't find the weather in " + location;
                document.getElementById("description").innerHTML = description;

            }
        }
    }
}

function setIconAndDescription(weather, location){

    var icon;
    var description;

    weather = weather.toLowerCase();

    if (weather == "clear sky"
        || weather == "clear"){

        icon = "clear.svg";
        description = "Yay, sunshine!";

    }
    else if (weather == "few clouds"){

        icon = "few-clouds.svg";
        description = "It's a little cloudy.";

    }

这个想法是使用颜色理论,即红色表示热,蓝色表示红色等等。

任何帮助都会很棒,

谢谢,

扎克

你的意思是改变页面的背景颜色?在那种情况下,只需使用 document.body.style.backgroundColor = "red";

我想你想在这个阶段做:

函数 setIconAndDescription(天气、位置){

var icon;
var description;

weather = weather.toLowerCase();

if (weather == "clear sky"
    || weather == "clear"){

    icon = "clear.svg";
    description = "Yay, sunshine!";
    document.getElementById("description").style.backgroundColor = 'your-color';

}
else if (weather == "few clouds"){

    icon = "few-clouds.svg";
    description = "It's a little cloudy.";
    document.getElementById("description").style.backgroundColor = 'your-color';
}

}