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