gpt4 book ai didi

Javascript - 尽管使用 onreadystatechange 还是出现 null 错误

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

。大家好,我正在尝试实现 this教程作为页面中的模块,但我收到“weatherData.Temperature is null”错误。

我之前收到过“weatherData.city is null”错误,但通过在 js 上使用 onreadystatechange 修复了该错误。

我在收到错误之前就明白了这一点,因为我试图将 InnerHTML 设置为 null,因为我没有将脚本包装在 onreadystatechange 中,但现在我很困惑为什么会收到类似的错误对于不同的代码行。

如果有人能告诉我我做错了什么,我将非常感激!太感谢了!

(是的,由于我无法控制的原因,我无法分离页面上的 html 和 js。我知道。我知道......)

HTML:

<div id="weather-web-app">
<section>
<h1 id="city">Weather Web App</h1>
<p>
<a href="temperature" href="#" onclick="switchUnits(); return false;" title="Click to switch between metric and imperial units">
</a>
<span id="weather">
Current temperature and weather conditions
</span>
</p>
</section>

</div>

JS

<script>
document.onreadystatechange = function(){
var weatherData = {
city: document.querySelector("#city"),
weather: document.querySelector("#weather"),
temperature: document.querySelector("#temperature"),
temperatureValue: 0,
units: "°C"
};

function switchUnits(){
if (weatherData.units == "°C"){
weatherData.temperatureValue = weatherData.temperatureValue * 9/5 + 32;
weatherData.units = "°F";
} else {
weatherData.temperatureValue = (weatherData.temperatureValue - 32) * 5/9;
weatherData.units = "°C";
}

weatherData.temperature.innerHTML = weatherData.temperatureValue + weatherData.units + ", ";
}

function getLocationAndWeather(){
if (window.XMLHttpRequest){
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function() {
var response = JSON.parse(xhr.responseText);

console.log(response);
var position = {
latitude: response.latitude,
longitude: response.longitude
};
var cityName = response.city;

var weatherSimpleDescription = response.weather.simple;
var weatherDescription = response.weather.description;
var weatherTemperature = response.weather.temperature;

weatherData.temperatureValue = weatherTemperature;

loadBackground(position.latitude, position.longitude, weatherSimpleDescription);

weatherData.city.innerHTML = cityName;
weatherData.weather.innerHTML = ", " + weatherDescription;
weatherData.temperature.innerHTML = weatherTemperature + weatherData.units;
}, false);

xhr.addEventListener("error", function(err){
alert("Could not complete the request");
}, false);

xhr.open("GET", "https://fourtonfish.com/tutorials/weather-web-app/getlocationandweather.php?owapikey=YOUROPENWEATHERMAPKEY&units=metric", true);
xhr.send();
} else {
alert("Unable to fetch location and weather data.");
}
}

function loadBackground(lat, lon, weatherTag) {
var script_element = document.createElement('script');

script_element.src = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=YOURFLICKRAPIKEY&lat=" + lat + "&lon=" + lon + "&accuracy=1&tags=" + weatherTag + "&sort=relevance&extras=url_l&format=json";

document.getElementsByTagName('head')[0].appendChild(script_element);
}

function jsonFlickrApi(data){
if (data.photos.pages > 0){
var photo = data.photos.photo[0];
document.querySelector("body").style.backgroundImage = "url('" + photo.url_l + "')";
document.querySelector("#image-source").setAttribute("href", "http://www.flickr.com/photos/" + photo.owner + "/" + photo.id);
} else {
document.querySelector("body").style.backgroundImage = "url('https://fourtonfish.com/tutorials/weather-web-app/images/default.jpg')";
document.querySelector("#image-source").setAttribute("href", "https://www.flickr.com/photos/superfamous/310185523/sizes/o/");
}
}

getLocationAndWeather();
}
</script>

最佳答案

您可能需要检查 onreadystatechange 中的 readyState

document.onreadystatechange = function () {
if (document.readyState === "interactive") {

// ...

getLocationAndWeather();
}
};

示例:document.readyState

<小时/>

你也需要改变

<a href="temperature" 

<a id="temperature" 
<小时/>

要运行 switchUnits,您需要

switchUnits = function switchUnits () {
if (weatherData.units == "°C"){
weatherData.temperatureValue = weatherData.temperatureValue * 9/5 + 32;
weatherData.units = "°F";
} else {
weatherData.temperatureValue = (weatherData.temperatureValue - 32) * 5/9;
weatherData.units = "°C";
}

weatherData.temperature.innerHTML = weatherData.temperatureValue + weatherData.units + ", ";
};

...因此该函数被发布到窗口

关于Javascript - 尽管使用 onreadystatechange 还是出现 null 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27510496/

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