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

标签 javascript jquery html variables toggle

我正在尝试使用该按钮从华氏温度更改为摄氏温度,反之亦然。我试图设法更改按钮上的文本和温度旁边的 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);

关于javascript - 使用该按钮作为切换按钮来更改文本并将一个变量替换为另一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50336301/

相关文章:

javascript - 如何在 python 中使用 selenium 获取包含在 HTML 标记中的 html 文档

javascript - 换行符 (\n) 在 IE 中不起作用

javascript - 输入文本上的 Jquery Datetimepicker 事件

javascript - 正则表达式捕获特定字符串之前的所有内容,然后逐步捕获

javascript - 调整窗口大小时重置 div 高度

javascript - 限制输入字段上图像的宽度和高度

javascript - TestCafe错误: "No tests to run. Either the test files contain no tests or the filter function is too restrictive"

javascript - 为什么 <th> 或 <td> 边框在 mozilla 和 IE 中没有显示(我在这些 <th> 或 <td> 上使用了 Jquery UI resizable() 函数),但在 chrome 中却工作正常?

javascript - 如何设计多字段的javascript提示框

html - 如何保留元素 :active even after clicking