使用按钮作为开关来更改文本并将一个变量替换为另一个变量
Use the button as a toggle to change text and replace one variable with another variable
我正在尝试使用按钮将华氏度更改为摄氏度,反之亦然。我试图设法更改按钮上的文本和温度旁边的 F/C 个词,但等式 $("#temp").text() === currentTempInCelsius
总是 return 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>
将 ===
更改为 ==
,如下所示:
$("#temp").text() == currentTempInCelsius ? $("#temp").text(fTemp) : $("#temp").text(currentTempInCelsius);
我正在尝试使用按钮将华氏度更改为摄氏度,反之亦然。我试图设法更改按钮上的文本和温度旁边的 F/C 个词,但等式 $("#temp").text() === currentTempInCelsius
总是 return 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>
将 ===
更改为 ==
,如下所示:
$("#temp").text() == currentTempInCelsius ? $("#temp").text(fTemp) : $("#temp").text(currentTempInCelsius);