我正在编写一个前端网络应用程序,为用户的位置提供天气条件。虽然基本功能正在运行,但目前我正在尝试编写允许应用根据当前天气状况更改body
背景的逻辑。为此,我正在使用OpenWeatherMap API。对此API的调用会返回JSON响应,并且在返回的许多内容中,有两个字段 - id
和icon
。 id
会返回一个数字代码,用于确定天气状况,例如晴朗的天空或破碎的云,icon
会返回一个字符串,用于确定是否&# 39;白天或晚上。
为了编写逻辑,我创建了一个JS对象,其id
条件代码为keys
,相应图像为values
。在.ajax()
函数中,我有switch
块,其中switch
基于返回的id
,然后使用.css("background-image", "url(link)");
设置背景图片身体。
代码的问题在于 - 这样浏览器似乎无法识别背景图像的URL。我在控制台中看到GET - ... 404 not found
错误。但是,如果我不是通过JS对象尝试访问图像的URL,而是直接将URL放在.css()
中的图像中,它就可以工作。但当然,这不是我想要的。
这是JS对象 -
//object for images
var weatherConditions = {
//Group 2xx: Thunderstorm
'2xx' : 'http://www.weathersnapshot.com/wp-content/uploads/2014/04/414.jpg',
//Group 3xx: Drizzle
'3xx' : 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSsE4vZDsOCCsuE9X2WljNm0VewUdFj7onj438c9mD3_I6AfT71sA',
//Group 5xx: Rain
'5xx' : 'https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSsAlLmRls8CtL994qiwssqvQwdMNEI7yJcUUPWqNhMdtjGHExK',
//Group 6xx: Snow
'6xx': 'https://dl.dropbox.com/s/sywq7fk1u65lycf/6810006-snow-background.jpg?dl=0',
//Group 800: Clear - day and night
'800': ['https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTnYxZWx1uOc83uWxrTFfW7IiVj0XXMKM6fSdqS9U5kjh7Op_71WA', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQnSe0ASBAUAcsvEAkQqO28WFGCjLWAgODq9eosh2SBr9f4-_NI4Q'],
//Group 80x: Clouds - day and night
'8xx' : ['https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQ_Fl9xNhT3AVwafZ2GeciqaqdLXosI-nPfhrVGvM7QFO0z0KQa', 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTgvOYSdZ7tHUybUfF5ydC2G97i78pv3hZ8jlX632lCu26VABGs3Q']
};
注意 - 我也尝试在Dropbox上托管图片,因为我认为错误可能是由于图片链接本身造成的。但它没有效果。
这是开关块的一部分 -
//---------get background-----------------
var weatherID = data.weather[0].id;
console.log("Weather ID = " + weatherID);
switch(weatherID) {
case 600: console.log(weatherConditions['6xx']);
$("body").css("background-image", "url(weatherConditions['6xx']");
break;
case 800: console.log("inside clear sky weather");
$("body").css("background-image", "url(weatherConditions.8xx[0])");
break;
case 803: //check for day or night
if(data.weather[0].icon.endsWith("d")) {
console.log("inside day weather");
$("body").css("background-image", "url(weatherConditions['8xx'][0])");
}
break;
case 804: $("body").css("background-image", "url(http://www.weathersnapshot.com/wp-content/uploads/2014/04/414.jpg)");
}
这里是Pen的完整代码。
答案 0 :(得分:1)
"url(weatherConditions['6xx']"
完全它的样子。那里没有替换,你告诉浏览器使用weatherConditions['6xx']
字面上作为URL。 (你也错过了)
。)
你可能想替换:
$("body").css("background-image", "url(" + weatherConditions['6xx'] + ")");
在ES2015中(你现在可以使用转换,以及几个月/一年/没有转发的东西)你可以使用模板字符串(引用反引号,使用${...}
作为占位符):
$("body").css("background-image", `url(${weatherConditions['6xx']})`);
以下是使用Gravatars的示例:
var gravatars = {
"Manish": "https://www.gravatar.com/avatar/4657faf6aa5c4b2c01ce4fb6c63300ed?s=164&d=identicon&r=PG",
"T.J.": "https://www.gravatar.com/avatar/ca3e484c121268e4c8302616b2395eb9?s=164&d=identicon&r=PG"
};
$("input[type=button]").on("click", function() {
$(".holder").css("background-image", "url(" + gravatars[this.value] + ")");
});
.holder {
display: inline-block;
width: 164px;
height: 163px;
}
<input type="button" value="Manish">
<input type="button" value="T.J.">
<div>
<div class="holder"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>