这个文件,上面写着
Uncaught (in promise) TypeError: Cannot set property 'textContent' of null
at app.js:25
window.addEventListener('load', () => {
let long;
let lat;
let temperatureTimeZone = document.querySelector("temperature-timezone");
let temperatureDegree = document.querySelector("temperature-degree");
let temperatureDescription = document.querySelector("temperature-description");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
long = position.coords.longitude;
lat = position.coords.longitude;
const proxy = `https://cors-anywhere.herokuapp.com/`;
const api = `${proxy}https://api.darksky.net/forecast/1299787a9e937056e2ee6cb21ebfc2d4/${lat},${long}`;
fetch(api)
.then(response => {
return response.json();
})
.then(data => {
console.log(data);
const { temperature, summary, precipType, icon } = data.currently;
// set Dom Elements from APi
temperatureDegree.textContent = temperature;
});
});
} else {
h1.textcontent = "Hey reload site and allow geolocation";
}
});
最佳答案
什么是“温度”?如果它是一个 id,那么你必须做“#temperature-degree”。如果它是一个类,那么你必须做“.temperature-degree”。如果它是一个标签,那么你没有任何符号,但“温度-度数”不应该是一个标签。如果您仍然感到困惑,请查看它的文档:developer.mozilla.org/docs/Web/API/Document/querySelector
关于javascript - 未捕获( promise )TypeError : Cannot set property 'textContent' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56241224/