简体   繁体   English

使用按钮作为切换按钮来更改文本并将一个变量替换为另一个变量

[英]Use the button as a toggle to change text and replace one variable with another variable

I am trying to use the button to change from fahrenheit to celcius and vice versa. 我正在尝试使用该按钮将华氏温度更改为摄氏温度,反之亦然。 I tried to manage to change the text on the button and F/C words next to temperature but the equation $("#temp").text() === currentTempInCelsius always return false 我试图更改按钮上的文本以及温度旁边的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> 

像这样将===更改为==

     $("#temp").text() == currentTempInCelsius ? $("#temp").text(fTemp) : $("#temp").text(currentTempInCelsius);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM