【发布时间】:2018-05-14 17:54:04
【问题描述】:
我正在尝试使用按钮将华氏温度更改为摄氏温度,反之亦然。我试图设法更改按钮上的文本和温度旁边的 F/C 字词,但方程式 $("#temp").text() === currentTempInCelsius 总是返回 false
$(document).ready(function(){
$("#changerToC").click(function () {
var fTemp = currentTempInCelsius * 9 / 5 + 32;
$("#temp").text() === currentTempInCelsius ? $("#temp").text(fTemp) : $("#temp").text(currentTempInCelsius);
$("#changerToC").text() ==="Change to Celcius" ? $("#changerToC").text("Change to Fahrenheit") : $("#changerToC").text("Change to Celcius");
$("#tempunit").text() === "C" ? $("#tempunit").text('F') : $("#tempunit").text('C');
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<h1 class="title">
Free C<i class="fa fa-mixcloud"></i>de Camp
<br>
Weather App
</h1>
<h2 id="city"></h2>
<h2 id="country"></h2>
<br><br>
<h2 id="temp"></h2>
<h2 id="tempunit"></h2>
<br><br>
<h2 id="desc"></h2>
<i id="weather-icon" class="fa"></i>
<br>
<button id="changerToC" class="btn btn-primary">Change to Fahrenheit</button>
</div>
【问题讨论】:
-
哪里出了问题?控制台错误?
-
它没有失败。我检查了控制台日志,但那里什么也没有。它只是没有改变温度
-
currentTempInCelsius传入/检索到哪里? -
奇怪,当我运行你的代码时会抛出
currentTempInCelsius is undefined。 -
对不起,我应该解释一下如何检索 currentTempInCelsius。它是从 API 动态检索的。所以收到它后,我试图用按钮将它从 fah 更改为 celcius,反之亦然
标签: javascript jquery html variables toggle