OpenWeather Api将JavaScript提取与用户输入配合使用,将以前的数据保留在HTML页面中

十夸西

我正在使用JavaScript Fetch从OpenWeather Api获取数据。我有一个表格供用户输入要查看其天气信息的城市。由于某些原因,来自前一个城市的数据仍会在HTML页面中弹出,而不是消失以代替新数据。如何清除内存,以使新搜索城市的新天气信息保留在页面中?以下是JS和HTML的代码

var weatherData = document.getElementById("weather_data");
  weatherData.addEventListener('click',function(e){
 e.preventDefault();
 var cityName = document.getElementById("cityName").value;
 var url = "https://api.openweathermap.org/data/2.5/weather?q="+cityName+"&appid=1b81668fc60a1d1905a3e5a311d45414";
  if(cityName == ""){
      alert("Enter a city name");
  }else{
  fetch(url).then(function(response){
      if(response.ok){
          return response.json();
      }else{
          throw new Error(Error);
      }
  }).then(function(data){
      console.log(data);
    const html =    `
        <h2 class="text-danger text-center"><span class="text-dark">City:</span>${data.name}</h2>
        ` ;
      document.getElementById("display_data").insertAdjacentHTML('afterbegin',html);
  }).catch(function(error){
      console.log(error);
  });
  }
});

HTML表格

<form>
 <input type="text" id="cityName" placeholder="Enter a city name"><br>
 <input type="submit" value="Get Weather Information" id="weather_data">
</form>
张凯文

Element接口的insertAdjacentHTML()方法将指定的文本解析为HTML或XML,并将结果节点插入到DOM树中的指定位置。它不会重新解析正在使用该元素的元素,因此不会破坏该元素内部的现有元素。这避免了额外的序列化步骤,使其比直接的innerHTML操作快得多。

var weatherData = document.getElementById("weather_data");
  weatherData.addEventListener('click',function(e){
 e.preventDefault();
 var cityName = document.getElementById("cityName").value;
 var url = "https://api.openweathermap.org/data/2.5/weather?q="+cityName+"&appid=1b81668fc60a1d1905a3e5a311d45414";
  if(cityName == ""){
      alert("Enter a city name");
  }else{
  fetch(url).then(function(response){
      if(response.ok){
          return response.json();
      }else{
          throw new Error(Error);
      }
  }).then(function(data){
    const html =    `
        <h2 class="text-danger text-center"><span class="text-dark">City:</span>${data.name}</h2>
        ` ;
      document.getElementById("display_data").innerHTML = html;
  }).catch(function(error){
      console.log(error);
  });
  }
});
<form>
 <input type="text" id="cityName" placeholder="Enter a city name"><br>
 <input type="submit" value="Get Weather Information" id="weather_data">
</form>

<div id="display_data"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用openweather API将json加载到熊猫数据框

使用Openweather API获取特定日期的天气-解析JSON响应

如何在数据表中绑定Openweather地图API数据?

使用套接字的Python3 OpenWeather API

OpenWeather API React请求标头字段太大431

为什么我的寻呼机适配器无法显示我从openweather加载的数据(以json格式)无法正确显示

Openweather.com API 5未来几天的预报问题

OpenWeather JSONP

OpenWeather:通过lat&lon查询历史数据

OpenWeather获取方法

OpenWeather:未获取数据

openweather API .getJSON请求返回未定义

如何在Ubuntu 18.04中升级gnome-shell-extension-openweather

如何用simpledate格式解析openweather数据

如何将 jQuery 数据处理更改为 openWeather API 的 Knockout 绑定

Openweather 扩展不起作用

OpenWeather API 的错误

OpenWeather API 的 JSON 结果集不返回 Swift 中主字典的值

OpenWeather API 中的无

解析 OpenWeather JSON 数据时出现 keyNotFound 错误 (SwiftUI)

如何使用 Flutter 中 OpenWeather API 的时区属性获取任何城市/国家的当前时间?

react.js 中的 Openweather api 调用失败

使用 JavaScript 获取 Openweather 预报

无法添加 URL 以使用 openweather 从 api 调用中获取数据

Kotlin Json 解析 openweather 数据时出错

如何在 kotlin 中从 openweather 响应将字符串转换为 JSONObject?

使用 Openweather Api 處理 react fetch

从 openweather api 中提取数据

OpenWeather One Call API 坐标未加载