gpt4 book ai didi

javascript - 类型错误 : Cannot set property 'textContent' of null ; temperatureDegree. 文本内容 = 温度

转载 作者:行者123 更新时间:2023-12-01 00:14:39 25 4
gpt4 key购买 nike

我正在制作一个天气应用程序,无法编辑“textContent”而不出现错误:temperDegree.textContent = temp

window.addEventListener("load", ()=> {
let long
let lat
let temperatureDescription = document.querySelector("temperature-description")
let temperatureDegree = document.querySelector("temperature-degree")
let locationTimezone = document.querySelector("location-timezone")

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
long = position.coords.longitude
lat = position.coords.latitude

const proxy = "http://cors-anywhere.herokuapp.com/"
const api = `${proxy}https://api.darksky.net/forecast/503a88031807535a11c0654c147002d5/${lat},${long}`
fetch(api)
.then(response => {
return response.json()
})
.then(data => {
console.log(data)
const {temperature, summary} = data.currently
//Set DOM Elements from the API
temperatureDegree.textContent = temperature
})
})
} else {
h1.textContent = "We can't find your location...make sure it is activated."
}
})

最佳答案

您必须位于选择器符号之前。如果温度-度是一个id,则在其前面加上散列(#)符号

let temperatureDegree = document.querySelector("#temperature-degree")

或者,如果它是一个,则在其前面加上点(.)符号

let temperatureDegree = document.querySelector(".temperature-degree")

您也必须对其他元素执行相同的操作。

关于javascript - 类型错误 : Cannot set property 'textContent' of null ; temperatureDegree. 文本内容 = 温度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59852721/

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