jQuery Button只能工作一次,无法将华氏温度变为摄氏温度

时间:2017-08-04 10:05:40

标签: javascript jquery

<!DOCTYPE html>
<html>
<head>
    <title>Local Weather</title>
    <script
    src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/weather-icons/2.0.10/css/weather-icons-wind.css">
    <link rel="stylesheet" type="text/css" href="Local Weather.css">
</head>
<body>
    <script type="text/javascript" src="Local Weather.js"></script>
    <div class="container">       
        <center>
            <h1 id="degree"></h1>           
            <h1 id="name"></h1>
            <h1 id="description"></h1>
        </center>
    </div>
</body>
</html>

var latitude, longitude, url, btn, temp;
var test = true;
$(document).ready(function()
{
    $.getJSON("https://freegeoip.net/json/", function(data)
    {
        latitude = data.latitude;
        longitude = data.longitude;
        url = "https://fcc-weather-api.glitch.me/api/current?lat="+latitude+"&lon="+longitude;
        $.getJSON(url, function(data2)
        {  
            temp = data2.main.temp;
            $("#degree").html(temp + '<button id="corf">&#8451;</button>');
            $("#name").html(data2.name);
            $("#description").html(data2.weather[0].description + '<img id="icon" src='+ data2.weather[0].icon + '/>');
            btn = $("#corf");
            btn.click(function ()
            {
                if(test)
                {
                    temp = (temp * 1.8) + 32;
                    $("#degree").html(temp + '<button id="corf">&#8457;</button>');
                    test = false;
                }
                else
                {
                    temp = (temp * 0.5556) - 32;
                    $("#degree").html(temp + '<button id="corf">&#8451;</button>');
                    test = true;
                }
            });
        });
    });
});

不能多次将摄氏温度改为华氏温度,这有什么不对?

2 个答案:

答案 0 :(得分:0)

您正尝试将click事件绑定到DOM中不存在的元素。要确保events绑定正确,您可以使用Event Delegation。只需将事件添加到父元素并检查event.target即可。如果event.targetbutton,则进行正确的计算。

var latitude, longitude, url, btn, temp;
var test = true;
$(document).ready(function() {
  $.getJSON("https://freegeoip.net/json/", function(data) {
    latitude = data.latitude;
    longitude = data.longitude;
    url = "https://fcc-weather-api.glitch.me/api/current?lat=" + latitude + "&lon=" + longitude;
    $.getJSON(url, function(data2) {
      temp = data2.main.temp;
      $("#degree").html(temp + '<button id="corf">&#8451;</button>');
      $("#name").html(data2.name);
      $("#description").html(data2.weather[0].description + '<img id="icon" src=' + data2.weather[0].icon + '/>');
      $('#degree').click(function(e) {
        if (e.target.id === 'corf') {
          var newTemp = 0;
          if (test) {
            newTemp = (temp * 1.8) + 32;
            $("#degree").html(newTemp + '<button id="corf">&#8457;</button>');
            test = false;
          } else {
            newTemp = temp;
            $("#degree").html(newTemp + '<button id="corf">&#8451;</button>');
            test = true;
          }
        }
      });
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <script type="text/javascript" src="Local Weather.js"></script>
  <div class="container">
    <center>
      <h1 id="degree"></h1>
      <h1 id="name"></h1>
      <h1 id="description"></h1>
    </center>
  </div>
</body>

答案 1 :(得分:0)

试试这个:

var latitude, longitude, url, btn, temp;
var test = true;
$(document).ready(function()
{
    $.getJSON("https://freegeoip.net/json/", function(data)
    {
        latitude = data.latitude;
        longitude = data.longitude;
        url = "https://fcc-weather-api.glitch.me/api/current?lat="+latitude+"&lon="+longitude;
        $.getJSON(url, function(data2)
        {  
            temp = data2.main.temp;
            $("#degree").html(temp + '<button id="corf">&#8451;</button>');
            $("#name").html(data2.name);
            $("#description").html(data2.weather[0].description + '<img id="icon" src='+ data2.weather[0].icon + '/>');
            btn = $("#corf");
            $(document).on('click',btn,function ()
            {
                if(test)
                {
                    temp = (temp * 1.8) + 32;
                    $("#degree").html(temp + '<button id="corf">&#8457;</button>');
                    test = false;
                }
                else
                {
                    temp = (temp - 32) / 1.8;
                    $("#degree").html(temp + '<button id="corf">&#8451;</button>');
                    test = true;
                }
            });
        });
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <script type="text/javascript" src="Local Weather.js"></script>
  <div class="container">
    <center>
      <h1 id="degree"></h1>
      <h1 id="name"></h1>
      <h1 id="description"></h1>
    </center>
  </div>
</body>