<!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>
标签:Weather,示例,color,32,天气,data,weather,weatherInfo,border
From: https://blog.51cto.com/u_16160172/11999140