简体   繁体   English

使用 API 创建天气应用程序,我正在尝试显示图标

[英]Creating a weather app using API and I am trying to show the icon

I have been fiddling with this code teaching myself javaScript and API.我一直在摆弄这段代码,教自己 javaScript 和 API。 I can retrieve the icon and get it to show doing document.body.innerHTML = imgUrl , but I can't get it to show in the specific div using the variable iconPlacement.我可以检索图标并让它显示做 document.body.innerHTML = imgUrl ,但我无法使用变量 iconPlacement 在特定 div 中显示它。 It says in the console that "Cannot set property 'innerHTML' of null".它在控制台中说“无法设置 null 的属性‘innerHTML’”。 Everything else works great I just can't get the icon to show up!其他一切都很好,我只是无法显示图标!

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');
    let iconPlacement = document.querySelector("icon");


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

            const api = `http://api.weatherapi.com/v1/current.json?key=#&q=${lat},${long}`;

            fetch(api)
            .then(response =>{
                return response.json();
            })
            .then(data =>{
                console.log(data)
                const {temp_f} = data.current;
                const {text} = data.current.condition;
                const {name,region} = data.location;
                const iconUrl = data.current.condition.icon;
                const imgUrl = ("<img src='https:" + iconUrl  + "'>");
                //set DOM elements from the API
                temperatureDegree.textContent = temp_f;
                temperatureDescription.textContent = text;
                locationTimeZone.textContent = name + "," + " " + region;
                iconPlacement.innerHTML = imgUrl;
                });

        
        });
    }

});

You didn't select the div, that's why you get the error message.您没有选择 div,这就是您收到错误消息的原因。 Try to select it with it's class or id name:尝试使用它的类或 id 名称选择它:

let iconPlacement = document.querySelector(".icon");  /*(by class)*/
let iconPlacement = document.querySelector("#icon");  /*(by id)*/

also I would suggest to make the api call an async function:我还建议使 api 调用异步函数:

const apiCall = async () => {
    const response = await fetch(/*url*/);
    const jsonRespons = await response.json();
    /*code to handle json*/
}

Just use a class or an id for your icon(s).只需为您的图标使用一个类或一个 id。

<div id="icon"></div>

Now you can do the following in jquery:现在您可以在 jquery 中执行以下操作:

$("#icon").html(imgUrl) ; $("#icon").html(imgUrl) ;

And always try to write the code in simplest way.并始终尝试以最简单的方式编写代码。 I would have never used query selector where I could use just $("#id)" .我永远不会使用查询选择器,我可以只使用$("#id)" If you still wanna use query selector, you have to go through a for each loop.如果你仍然想使用查询选择器,你必须经过一个 for each 循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM