0
function getWeather(lat, lon){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
currentTempInCelsius =parseInt(myObj.main.temp);
document.getElementById("temp").innerHTML =currentTempInCelsius;
backImage(parseInt(myObj.main.temp));
}
};
xmlhttp.open("GET", urlString, true);
xmlhttp.send();
}
在第一個函數中,我從API獲取當前溫度,然後將其解析並傳遞給backImage。然後,我應用一個if循環來分配一個css類(.cold,.avg & .body),它將url保存到背景圖像中,但它似乎不起作用。無法根據JSON API變量獲取HTML背景
function backImage(tempp){
if(tempp<15){
setImg(cold);
}
if(tempp>=15&&tempp<25){
setImg(avg);
}
if(tempp>=25){
setImg(hot);
}
}
function setImg(myClass){
$('body').className = myClass;
}
似乎沒有工作,雖然它是有道理的,首先刪除類 –