原生js获取天气信息——以和风天气为例

首先要去到对应网站的控制台,查看请求路径和参数,然后直接上源码,这里只展示js,对应的html元素不明白怎么写的话问下AI就行了。

document.addEventListener("DOMContentLoaded", function() {
    const textP = document.getElementById("text");
    const weatherImg = document.getElementById("weatherImg");
  
  
    const humidityP = document.getElementById("humidity");
    const windDirP = document.getElementById("windDir");
    const temp= document.getElementById("temp");
    getResult();
  
    function getResult() {
      const cityID = "101281001"; 
      const url = `https://devapi.qweather.com/v7/weather/now?location=${cityID}&key=your_key`; 
      fetch(url)
        .then(response => response.json())
        .then(data => updateUI(data))
        .catch(() => textP.innerHTML = "获取天气信息失败,请检查城市天气ID");
    }
  
    function updateUI(data) {
      const { now } = data;
      textP.innerHTML = now.text;
      
     
      humidityP.innerHTML = now.humidity;
      windDirP.innerHTML = now.windDir;
      temp.innerHTML 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值