<div id="weather-container"></div>
const apiKey = '你的 API 密钥';
const city = '你要查询的城市名称';
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
fetch(url)
.then(response => response.json())
.then(data => {
// 解析天气数据
const temperature = data.main.temp;
const humidity = data.main.humidity;
const description = data.weather[0].description;
// 在页面上显示天气信息
const weatherHTML = `
<p>温度:${temperature}°C</p>
<p>湿度:${humidity}%</p>
<p>天气状况:${description}</p>
`;
document.getElementById('weather-container').innerHTML = weatherHTML;
})
.catch(error => {
console.error('获取天气数据失败:', error);
});
你的 API 密钥
替换为你在 OpenWeatherMap 注册后获得的 API 密钥,并将你要查询的城市名称
替换为实际的城市名称。
文章来自深蓝互联http://www.szdbi.com/WEBkaifajishu/531.html转载请注明出处!
下一篇:开源的即时聊天解决方案