javascript
  • jquery
  • click
  • 2017-04-06 59 views 0 likes 
    0

    我有這個天氣頁面,每次我點擊按鈕它從Celcius變爲華氏溫度或反之亦然。按鈕被觸發兩次,元素不再被渲染

    發生的事情是,一旦我第一次點擊,它就可以正常工作,但如果再次單擊它,我的控制檯日誌會顯示它執行了兩次並且不再呈現該元素(#link )。

      $("#data").on('click', '#link', function() { 
          var html2 = ""; 
          html2 += '<button class="temp" id="link">' 
          if (flag == 0){ 
           console.log("c to f"); 
           html2 += "<h1>" + celciusToFahrenheit(Math.round(json.main.temp)) + " °F</h1>"; 
           flag = 1; 
          } else if (flag == 1){ 
           console.log("f to c"); 
           html2 += "<h1>" + Math.round(json.main.temp) + " °C</h1>"; 
           flag = 0; 
          } 
          html2 += "</button>" 
          $("#link").html(html2); 
         }); 
    

    我在內的整個文件波紋管:

    $(document).ready(function() { 
     
        function getCurrentLocation(callback) { 
     
         if (!navigator.geolocation) return; 
     
         navigator.geolocation.getCurrentPosition(function(position) { 
     
          latitude = position.coords.latitude; 
     
          longitude = position.coords.longitude; 
     
          url = ('http://api.openweathermap.org/data/2.5/weather?lat=' + latitude + '&lon=' + longitude + '&units=metric&appid=b464bb8dd84e7e7d36103593a472ae9a'); 
     
          callback(url); 
     
         }); 
     
        } 
     
    
     
        function celciusToFahrenheit(celcius) { 
     
         var fahrenheit = celcius * (9/5) + 32; 
     
         return fahrenheit; 
     
        } 
     
    
     
        getCurrentLocation(function(currLocMap) { 
     
         $.getJSON(url, function(json) { 
     
          var html = ""; 
     
          var flag = 0; 
     
    
     
          html += '<button class="temp" id="link">' 
     
          html += "<h1>" + Math.round(json.main.temp) + " °C </h1>"; 
     
          html += "</button>" 
     
    
     
          html += "<h1>" + json.name + "</h1>"; 
     
          html += "<h3>" + json.weather[0].main + "</h3>"; 
     
          html += "<h3>" + json.weather[0].description + "</h3>"; 
     
    
     
    \t \t \t $("#data").on('click', '#link', function() { 
     
    \t \t \t \t var html2 = ""; 
     
    \t \t \t \t html2 += '<button class="temp" id="link">' 
     
    \t \t \t \t if (flag == 0){ 
     
    \t \t \t \t \t console.log("c to f"); 
     
    \t \t \t \t \t html2 += "<h1>" + celciusToFahrenheit(Math.round(json.main.temp)) + " °F</h1>"; 
     
    \t \t \t \t \t flag = 1; 
     
    \t \t \t \t \t console.log(flag); 
     
    \t \t \t \t } else if (flag == 1){ 
     
    \t \t \t \t \t console.log("f to c"); 
     
    \t \t \t \t \t html2 += "<h1>" + Math.round(json.main.temp) + " °C</h1>"; 
     
    \t \t \t \t \t flag = 0; 
     
    \t \t \t \t \t console.log(flag); 
     
    \t \t \t \t } 
     
    \t \t \t \t html2 += "</button>" 
     
    \t \t \t \t $("#link").html(html2); 
     
    \t \t \t }); 
     
    
     
          console.log(json); 
     
          console.log(json.name); 
     
          console.log(json.main.temp); 
     
          console.log(json.weather[0].main); 
     
          console.log(json.weather[0].description); 
     
          console.log(json.weather[0].icon); 
     
    
     
          $("#data").html(html); 
     
         }); 
     
        }); 
     
    });
    button#link { background:none;border:none; }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
     
    <!DOCTYPE html> 
     
    <html lang="en"> 
     
    
     
    <head> 
     
        <meta charset="utf-8"> 
     
        <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
     
        <title>Weather</title> 
     
        <!-- Latest compiled and minified CSS --> 
     
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
     
        <!-- jQuery library --> 
     
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> 
     
        <!-- Latest compiled JavaScript --> 
     
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
     
        <link rel="stylesheet" href="style.css"> 
     
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
     
        <script type="text/javascript" src="script.js"></script> 
     
        <!-- <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script> --> 
     
    </head> 
     
    
     
    <body> 
     
        <div id="data"> 
     
         <h4>You are here:</h4> 
     
        </div> 
     
    
     
    
     
    </body> 
     
    
     
    </html>

    +0

    請附上您的HTML代碼 –

    +1

    那是因爲你是渲染裏面本身相同的元素?你能否向我們展示HTML代碼以獲得更好的清晰度?此外,一個頁面不能有2個元素具有相同的標識符#JustAnFYI –

    +0

    該html非常空。我只是用一個實際上我從天氣API – Zzeks

    回答

    0

    那是因爲你要重新單擊事件處理程序中的按鈕。您應該更改h1標籤的內容,您的處理程序,而不是重新創建整個鍵,像這樣:

    $("h1",$(this)).html("new content"); 
    
    +0

    得到的東西替換了一個div#數據,謝謝你完全工作!你知道如何解釋爲什麼創建一個新按鈕使它無法工作了?如果我確實需要創建其他按鈕,那我該如何讓它們工作? – Zzeks

    相關問題