我正在尝试使用Open Weather Map API创建天气应用。我想创建一个按钮,使您可以在华氏温度和摄氏温度之间切换。我已经尝试了所有内容,但是在尝试按下按钮之前,我已经回到编写的代码。

如何在当前设置中实现此功能?

<div class="container">

    <div class="jumbotron text-center"
style="background-color: #00F0F8FF; font-family: 'Oswald', sans-
serif; color: black;">
        <h1>Local Weather App</h1>
        <h2><span id="town"></span></h2>
        <h2>Temperture: <span id="temp"></span></h2>
        <div id="weatherIconBox"></div>
        <h2><span id="weatherType"></span></h2>
        <button type="submit" id="btn1">F&#176;</button>
        <button type="submit" id="btn2">C&#176;</button>

    </div>
</div>

var getIP = 'http://ip-api.com/json/';
var openWeatherMap = 'http://api.openweathermap.org/data/2.5/weather'
$.getJSON(getIP).done(function(location) {
$.getJSON(openWeatherMap, {
    lat: location.lat,
    lon: location.lon,
    units: "imperial",
}).done(function(data) {
    $('#town').html(data.name);
    $('#temp').prepend(Math.floor(data.main.temp) + '&#176;');
$('#weatherType').html(data.weather[0].description).css('textTransform', 'capitalize');
$('#weatherIconBox').prepend('<img id="weatherIcon"     src="http://openweathermap.org/img/w/' + data.weather[0].icon +     '.png"/>');

    });
});

最佳答案

我也在使用API​​创建辅助项目。

你可以试试这个吗默认情况下,以华氏温度显示数据。然后单击“ Celcius”切换按钮。做一个简单的数学计算。我认为方程是((f + 40)÷1.8)− 40 = c。然后显示此值。

10-06 08:16