javascript - 尝试从 HTML 元素中的 api 数据格式化文本
问题描述
从天气 api 站点调用数据。让控制台记录数据以确保它确实显示出来。但似乎无法用来自 api 的数据替换 HTML 中的元素。
我对需要使用哪些函数将占位符文本替换为来自 api 的数据感到困惑。我尝试过innerHTML、textContent 和innerText。我要么用 innerHTML 未定义,要么用 textContent 什么都没有。
const api = `https://api.weather.io/v1.0/current?city=NewYork,NY&key=`;
let temperature = document.getElementById('temp');
fetch (api)
.then(response => {
return response.json();
})
.then(data => {
console.log(data);
temperature.innerHTML = data.temp ;
});
api数据记录如下:
数据:0:rh 57 pod "d" lon -78.63861 pres 1007.3 timezone "America/New_York" ob_time "2019-10-28 17:40" country_code "US" cloud 0
我希望变量(温度)等于 api 数据的字符串值。
解决方案
推荐阅读
- flutter - 我想让我的代码响应任何屏幕尺寸的颤振飞镖
- ios - Firebase Crashlytics 没有出现在 Xamarin.Forms 应用程序 (iOS) 中,尽管构建成功并上传了 dsym
- javascript - 为什么我在上传到 s3 时在终端中收到此错误?我写的不对?
- visual-studio-code - VSCode - 光标在 AltGr 上向左移动。如何解决?
- java - 在 Spring Boot WebFlux 中读取 postman 上传的 Excel 文件
- visual-studio - 在visual studio 2019 intellisense提供的建议列表中上下移动的快捷键
- javascript - 使用下拉菜单更改图像,仅适用于添加的最后一张图像
- connection - snowflake snowsql 错误 250003 (n/a): 未能获得响应。绞刑?
- html - 如何制作带有图像和标题的容器
- flutter - TweenAnimationBuilder 没有正确重复 Flutter