首页 > 其他分享 >示例 32: 简单的天气应用

示例 32: 简单的天气应用

时间:2024-09-13 09:51:58浏览次数:3  
标签:Weather 示例 color 32 天气 data weather weatherInfo border

<!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

相关文章

  • stm32之硬件SPI读写W25Q64存储器应用案例
    系列文章目录1.stm32之SPI通信协议2.stm32之软件SPI读写W25Q64存储器应用案例3.stm32之SPI通信外设文章目录系列文章目录前言一、电路接线图二、应用案例代码三、应用案例代码分析3.1基本思路3.2相关库函数介绍3.3MySPI模块3.3.1模块初始化3.3.2SPI基本时序......
  • 百度地图SDK Android版开发 11 覆盖物示例 4 线
    百度地图SDKAndroid版开发11覆盖物示例4线前言界面布局MapPolyline类常量成员变量初始值创建覆盖物移除覆盖物设置属性加载地图和释放地图MapPolylineActivity类控件响应事件运行效果图前言文本通过创建多个不同线宽的折线和大地曲线,介绍Polyline的使用方法。......
  • P3327 [SDOI2015] 约数个数和
    [SDOI2015]约数个数和题目描述设\(d(x)\)为\(x\)的约数个数,给定\(n,m\),求\[\sum_{i=1}^n\sum_{j=1}^md(ij)\]输入格式输入文件包含多组测试数据。第一行,一个整数\(T\),表示测试数据的组数。接下来的\(T\)行,每行两个整数\(n,m\)。输出格式\(T\)行,每行一个整数,表......
  • stm32 SPI通信协议&W25Q64(软件SPI读写W25Q64)
    理论SPI1.SPI通信SPI(SerialPeripheralInterface)是由Motorola公司开发的一种通用数据总线四根通信线:SCK(SerialClock)、MOSI(MasterOutputSlaveInput)、MISO(MasterInputSlaveOutput)、SS(SlaveSelect)同步,全双工支持总线挂载多设备(一主多从)SCK:时钟线MOSI:主机输出,从......
  • 使用WebSocket协议实现在ESP32上音频接收播放
     主要目的:学习WebSocket通讯协议和ESP32开发所需配置:Pycharm,python3.12,ESP32S3N16R8, 扬声器(8欧,2W), 功放模块:MAX98357I2SAMP。一、介绍 1、WebSocket协议 WebSocket是一种网络通信协议,位于OSI模型的应用层。它提供了在单个TCP连接上进行全双工通信的能力,使得客......
  • 使用Arduino Uno作为烧录器为Atmega328PB芯片直接烧录程序
    目录摘要烧录方式操作过程准备工作将Arduinouno设置为烧录器烧录器和目标板电路连接添加第三方库设置参数程序烧写使用ArduinoIDE进行烧写使用Ardudess进行烧写成果展示摘要通过将一块ArduinoUno设置成ISP模式作为烧录器,从而实现为ArduinoProMini空白的ATMega328PB芯片直接......
  • P3267 [JLOI2016/SHOI2016] 侦察守卫 题解
    P3267[JLOI2016/SHOI2016]侦察守卫题解\(n\le5\times10^5,D\le20\)的数据范围显然想到\(O(nd)\)的树形dp。考虑\(d\)这一维的状态设计。考虑\(i\)子树中的情况分为全部被覆盖和未全部被覆盖两种。对于第一种,显然我们要考虑子树中能向上覆盖影响的点的个数,于是设......
  • Python 内置函数汇总(使用示例)
    abs(number)求解整数,浮点数绝对值,返回复数模大小aiter()异步的方式遍历可迭代对象的异步迭代器importasynciocount=0asyncdefexample():asyncdefasync_generator():globalcountwhileTrue:count+=1......
  • 和平精英游戏启动错误?和平精英_323393.exe找不到入口的终极解决方案
    针对“和平精英游戏启动错误?和平精英_323393.exe找不到入口”的问题,这里提供一套终极解决方案,旨在帮助玩家快速定位问题根源并修复,从而恢复游戏的正常运行。终极解决方案1.检查系统文件完整性使用系统文件检查器(SFC):打开命令提示符(以管理员身份运行),输入sfc/scannow命令并......
  • 通用USB转RS-485/422 转换器-DAM3232N
    一、概述    DAM-3232N是一款通用的USB/RS-485/422转换器,无需外加电源、兼容USB、RS-422、RS-485标准,能够将单端的USB信号转换为平衡差分的RS-422或RS-485信号,提供每线浪涌保护功率,以及各种原因在线路上产生的浪涌电压并且极小的极间电容保证了RS-485/RS-42......