gpt4 book ai didi

javascript - 为什么调用函数更新后背景没有更新

转载 作者:行者123 更新时间:2023-12-03 05:19:55 25 4
gpt4 key购买 nike

我目前正在尝试根据存储的 ajax 响应的某些值修改正文的背景。但是当调用函数 getBackground(); 时它没有更新背景,我不明白为什么?我将不胜感激任何帮助,

  $(document).ready(function(){
// Ajax call

var responseWeather = $.parseJSON($.ajax({
url: urlLatLong,
dataType: "json",
async: false
}).responseText);


function getBackground(){
var n = new Date();
var hour = n.getHours();


if (hour > 6 && hour < 18) {
//response from Ajax
if (responseWeather.weather[0].main === 'Rain') {
$('body').css('background-image', "https://4.bp.blogspot.com/-FRc-pTtmDaE/VESPnFw7e2I/AAAAAAABSIw/g_YMwPEPvRQ/s0/Rain%2BDrops%2BWindow%2BUHD.jpg");
}else if (responseWeather.weather[0].main === 'Clouds') {
$('body').css('background-image', "http://wallpapersonthe.net/wallpapers/b/3840x2400/3840x2400-sky_nature_cloudy_day_in_the_alpine_jungle_cloud_mountain_jungle-30477.jpg");
}else if (responseWeather.weather[0].main === 'Clear') {
$('body').css('background-image', "https://wallpaperscraft.com/image/sun_light_beams_meadow_glade_summer_day_53997_3840x2400.jpg");
}
}else{

if (responseWeather.weather[0].main === 'Rain') {
$('body').css('background-image', 'https://images3.alphacoders.com/279/279307.jpg');
}else if (responseWeather.weather[0].main === 'Clouds') {
$('body').css('background-image', 'http://www.walldevil.com/wallpapers/a68/night-background-cloudy-wallpaper-animated-skies-sky-wallpapers-art.jpg');
}else if (responseWeather.weather[0].main === 'Clear') {
$('body').css('background-image', 'https://s-media-cache-ak0.pinimg.com/originals/c6/28/0e/c6280e8f909cdaa96f599f4b611e876c.jpg');
}
}
}
getBackground();


});

最佳答案

不要忘记将图像 url 包装到 url() 中。

$('body').css('background-image', "url(https://...)");

关于javascript - 为什么调用函数更新后背景没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41434170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com