如何修改JS Openweathermap温度值

问题描述 投票:0回答:1

我想为我的工作项目重新调整以下 openweathermap JS 天气应用程序的用途:

https://codepen.io/stack-findover/pen/vYgQrPP

该应用程序默认温度值为摄氏度,我想将其修改为默认华氏度。

我尝试修改以下几行,但没有成功。主要问题是,我不明白代码如何计算摄氏度与华氏度。

$("#cur-deg").html(Math.round(result.list[0].main.temp - 273.15)+"°C");

 $next.eq(i-1).find(".next-deg").html(Math.round(result.list[i].main.temp - 273.15)+"°C");

  if(t=="cel") {
      $this.html(Math.round((weatherData.list[i].main.temp*(9/5)) - 459.67)+"°F");
      $this.attr("data-temp", "far");
    } else {
      $this.html(Math.round(weatherData.list[i].main.temp - 273.15)+"°C");
      $this.attr("data-temp", "cel");

我还尝试修改 openweathermap url 以包含 &units=imperial.. 但这不会覆盖自动通过 IP 返回温度的代码。

var api = "https://api.openweathermap.org/data/2.5/forecast?lat=42.1237&lon=-71.1787&units=imperial";

免责声明,我对 JavaScript 完全是菜鸟。

感谢您提供的任何帮助。

javascript html css openweathermap codepen
1个回答
0
投票

尝试改变

$("#cur-deg").html(Math.round(result.list[0].main.temp - 273.15)+"°C");

$("#cur-deg").html(Math.round((result.list[0].main.temp * (9/5)) - 459.67)+"°F");

同样

$next.eq(i-1).find(".next-deg").html(Math.round(result.list[i].main.temp - 273.15)+"°C");

成为

$next.eq(i-1).find(".next-deg").html(Math.round((result.list[i].main.temp * (9/5)) - 459.67)+"°F");

请注意,您应该将 html 中的所有 data-temp 属性设置为“far”,因为这将是默认状态。

© www.soinside.com 2019 - 2024. All rights reserved.