javascript - 切换温度值Jquery

标签 javascript jquery

所以我想通过点击温度来来回切换,我尝试了一些方法,但出现“递归过多”错误。当我切换到开尔文时有效,但不知道如何切换回摄氏度。任何帮助表示赞赏。

这是我的 html:

<p style="cursor: pointer">Temperature: <span id="temperature" class="toggle"></span></p>

这是 JavaScript:

var tempKelvin = data.main.temp;
var tempCelsius = Math.round(((data.main.temp) - 273.15));

$("#temperature").html(tempCelsius + 'C');

$("#temperature").click(function(){
    $(".toggle").html(Math.round(tempKelvin) + 'K');
}); 

最佳答案

试试这个......

var tempKelvin = data.main.temp;
var tempCelsius = Math.round(((data.main.temp) - 273.15));

$("#temperature").html(tempCelsius + 'C');

$("#temperature").click(function(){
if ($(".toggle").html().indexOf('K')==-1)
{
    $(".toggle").html(Math.round(tempKelvin) + 'K');
}

else
{
$("#temperature").html(tempCelsius + 'C');
}
});

关于javascript - 切换温度值Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36423198/

相关文章:

javascript - 如何使用带有 require 和模块导出的 npm 包作为普通 JS 库

jquery - 无法将单选按钮与文本对齐

jquery - 在悬停 img 上显示 div

jquery - Fancybox jQuery 中的 CSS 渐变?

HTML 表格单元格中的 Javascript 函数调用

javascript - 将图像上传为数组

jquery - 如何处理书签和绝对定位的div

jquery - 嵌套的 jQuery 步骤 - Main Next 上没有任何显示

javascript - jQuery:setInterval 函数无故停止工作

javascript - 返回多个模板