调用函数更新后,为什么后台没有更新

时间:2017-01-02 22:47:36

标签: javascript jquery ajax

我目前正在尝试根据存储的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();


});

1 个答案:

答案 0 :(得分:1)

请勿忘记将图片网址打包到url()

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