Ich möchte eine Variable "Stadt" global und verwenden Sie es in anderen Funktionen. Ich bin ein JSON-Daten anfordert, und es scheint, in dieser FunktionJSON-Variable undefiniert, wenn global
var city;
function conditions(data) {
var city = data.current_observation.display_location.full;
var wcode = data.current_observation.icon;
$('#city').append('<h2>' +'Weather forecast in ' + city + '</h2>');
$('#city').addClass('headings-style');
}
Aber zu arbeiten WHE Ich versuche variable Stadt in anderer Funktion verwenden Ich erhalte ein undefinierten Daten:
function forecastDays(info) {
var locationOne = info.forecast.simpleforecast.forecastday;
locationOne.forEach(function (daysPlus) {
var high = daysPlus.high.celsius;
var low = daysPlus.low.celsius;
var arr = ["day1", "day2", "day3", "day4"];
jQuery.each(arr, function (i, val) {
$("#temp_" + val).html('<p>' + city + 'High: ' + high + '°C' + '<br>' + ' Low: ' + low + '°C' + '</p>');
});
});
Bitte , Hilfe.
Sie erneut declare ':
Im Beispiel unten, die
city
Variable korrekt im globalen Bereich, in dem dietown
Variable neu definiert wird in der Funktion Umfang verwendet werden Stadt "innerhalb" Bedingungen ". Tun Sie 'city = ...' anstelle von 'var city = ...'. – ideaboxerEine gute Frage! Es ist ein Fehler, der oft gemacht wird, und deshalb ist es gut, dass darauf geachtet wird. –