openweathermap api在IE11中不起作用

用户名

我使用jQuery Mobile来从openweathermap.org获取天气数据并显示它。它可以在IE10上正常工作,但不能在IE11上工作。可能是什么原因?(它在Chrome中有效。)您可以在http://jsfiddle.net/Gajotres/frSsS/上查看代码

$(document).on('pageinit', '#index', function(){        
    $(document).on('click', '#city-search-btn', function(){ 
       var cityName = $('#city-search').val();
       if(cityName.length > 0) {
         var url = 'http://api.openweathermap.org/data/2.5/weather?q='+cityName+'&units=metric';    
          $.ajax({
              url: url,
              dataType: "jsonp",
              async: true,
              beforeSend: function() {
                 // This callback function will trigger before data is sent
                 $.mobile.loading('show', {theme:"a", text:"Please wait...", textonly:false, textVisible: true}); // This will show ajax spinner
              },
              complete: function() {
                 // This callback function will trigger on data sent/received complete
                 $.mobile.loading('hide'); // This will hide ajax spinner
              },                
              success: function (result) {
                  ajax.parseJSONP(result);
              },
              error: function (request,error) {
                  alert('Network error has occurred please try again!');
              }
         });          
       } else {
             alert('Please enter city name!');
       }       
    });        
});

$(document).on('pagehide', '#map', function(){   
    $(this).remove();
});

$(document).on('pageshow', '#map',function(e,data){   
    var minZoomLevel = 12;

    var myLatLng = new google.maps.LatLng(weatherData.response.coord.lat, weatherData.response.coord.lon);

    var map = new google.maps.Map(document.getElementById('map_canvas'), {
       zoom: minZoomLevel,
       center: myLatLng,
       mapTypeId: google.maps.MapTypeId.ROADMAP
    });

   var image = {
       url:  'http://openweathermap.org/img/w/'+weatherData.response.weather[0].icon+'.png'
   };

   infoWindow = new google.maps.InfoWindow();
   infoWindow.setOptions({
       content: "<div class='info-window'><div class='icon-holder'><img src='http://openweathermap.org/img/w/"+weatherData.response.weather[0].icon+".png'/></div><div class='info-holder'><span class='info-text'>City:</span><br/>"+weatherData.response.name+"<br/><span class='info-text'>Min. Temp:</span><br/>"+weatherData.response.main.temp_min+" °C<br/><span class='info-text'>Temp:</span><br/>"+weatherData.response.main.temp+" °C<br/><span class='info-text'>Max. Temp:</span><br/>"+weatherData.response.main.temp_max+" °C</div></div>",
       position: myLatLng,
    });
   infoWindow.open(map);     

});

var ajax = {  
    parseJSONP:function(result){  
        weatherData.response = result;
      //alert(JSON.stringify(weatherData.response.weather[0].icon));
       var mapPage    =   $('<div>').attr({'id':'map','data-role':'page'}).appendTo('body');
    var mapHeader  = $('<div>').attr({'data-role':'header', 'data-theme' : 'b','id':'map-header'}).appendTo(mapPage);
    $('<h3>').html(weatherData.response.name + ' weather').appendTo(mapHeader);
    $('<a>').attr({'href':'#index', 'class' : 'ui-btn-righ'}).html('Back').appendTo(mapHeader);
    var mapContent = $('<div>').attr({'data-role':'content'}).appendTo(mapPage);
    $('<div>').attr({'id':'map_canvas', 'style':'height:100%'}).appendTo(mapContent);
    $.mobile.changePage( "#map", { transition: "slide"});
    }
}

var weatherData = {
    response : null
}
维克多·莱文

我不知道这有多大帮助,也许其他人会提供更好的解释。

首先,看起来pageinit自jQuery.mobile 1.4.0起就已贬值:https ://api.jquerymobile.com/pageinit/他们建议将其替换为pagecreate

但是问题很明显:IE11中pageinit没有pagecreate触发。因此,按钮onlcick永远不会被绑定。不确定是IE错误还是jsFiddle的...

简单地更换pageinit$(document).ready修复问题对我来说。参见小提琴:http : //jsfiddle.net/frSsS/54/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章