<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather App</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
box-sizing: border-box;
background-color: #f0f2f5;
}
.container {
max-width: 600px;
margin: 50px auto;
padding: 20px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
text-align: center;
}
h1 {
color: #333;
}
.form-group {
margin-bottom: 15px;
}
.form-group input {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
width: calc(100% - 22px);
}
button {
padding: 10px 20px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: white;
font-size: 1em;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.weather-info {
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>Weather Application</h1>
<div class="form-group">
<input type="text" id="city" placeholder="Enter city name">
</div>
<button onclick="getWeather()">Get Weather</button>
<div id="weatherInfo" class="weather-info"></div>
</div>
<script>
function getWeather() {
const city = document.getElementById('city').value;
const apiKey = 'YOUR_API_KEY_HERE'; // Replace with your API key
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 weatherInfo = document.getElementById('weatherInfo');
if (data.weather) {
weatherInfo.innerHTML = `
<h2>Weather in ${data.name}</h2>
<p>Temperature: ${data.main.temp}°C</p>
<p>Weather: ${data.weather[0].description}</p>
`;
} else {
weatherInfo.innerHTML = '<p>City not found</p>';
}
})
.catch(error => {
document.getElementById('weatherInfo').innerHTML = '<p>Failed to fetch weather data</p>';
});
}
</script>
</body>
</html>
示例 32: 简单的天气应用
原创
©著作权归作者所有:来自51CTO博客作者避风塘主的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:示例 33: 响应式导航菜单
下一篇:示例 31: 个人博客首页
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
AJAX的简单应用(加法运算示例)
1.首先创建Web空文件2.在页面中添加三个按钮,并命名为num1,num2,
xml 文本框 microsoft -
java应用dockerfile脚本简单示例
【代码】java应用dockerfile脚本简单示例。
java 开发语言 jar docker spring -
【STM32】FreeRTOS列表应用示例
00. 目录文章目录00. 目录01. 概述02. 任务设计03. 相关设置04. 程序设计05. 实验结果06. 附录07. 参考01. 概述掌握FreeRTOS中列表和列表项相关函数的使用。02. 任务设计3 个任务:start_task、task1_task 和task2_task,这三个任务的任务功能如下:start_task:用来创建其他2 个任务。task1_task:应用任务1,控制LED0 闪烁,用来提示系统正在运行。task2_task: 列表和列表项操作任务,调用列表和列
FreeRTOS FreeRTOS列表 FreeRTOS列表项 列表示例 列表应用 -
QT 实现简单的天气 哈哈
通过上面的代码就可以实现简单的网络请求服务端来展示简单的天气情
qt 开发语言 ci #include json