不知道問題出在哪裏,但在選定的div中沒有顯示任何內容。無法從openWeatherMap api獲取數據JavaScript
$(document).ready(function(){
var lat, lng, data;
// get the current location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
lat = position.coords.latitude;
lng = position.coords.longitude;
});
}else {
alert('Geo Location feature is not supported in this browser.');
}
//get the data from the openWeatherMap API
function getData(){
$.getJSON('api.openweathermap.org/data/2.5/weather?lat='+lat+'&lon='+lng+'', function(json){
data = json;
});
}
$('button').on('click', function(){
getData();
$(this).remove();
$('.container').removeClass('hidden');
$('#location').appendTo(data.name);
$('#Weather-condition').appendTo(data.weather.main);
$('#wind-speed').appendTo(data.wind);
});
});
你檢查任何錯誤控制檯成功的數據顯示? – yuriy636
沒有錯誤@ yuriy636。 – SenDjasni
您是否從您發佈的代碼以外的地方調用'getData()'?請張貼您的HTML。 – yuriy636