2017-11-23 5 views
0

私はそれをしようとしているので、ユーザーが文字をクリックすると、天気予報タイプが変わり、温度が変換されます。 たとえば、#weatherTypeをクリックすると、それは華氏 に変更したいので、celciusに戻るときは、再び#weatherTypeをクリックします。 しかし、クリックすると何も起こりません。私は間違って何をしていますか?JqueryどのようにあなたのHTMLのIDから文字列を取得し、別の文字列と比較しますか?

編集は:ここで私のhttps://codepen.io/angelbenoit/pen/RjJPyK

$('#weatherType').click(function() { 
       var check = $('#weatherType').text(); 
       if(check === "C"){ 
        $('#weatherType').html("F"); 
        $('#convertTemperature').html(fahrenheit); 
       } 
       else{ 
        $('#weatherType').html("C"); 
        $('#convertTemperature').html(celcius); 
       } 
      }); 
+3

[mcve]を含めるように質問を編集してください。 – j08691

+0

あなたは間違ったことをどういう意味ですか? 'fahrenheit'と' celcius'の関数や変数は、以前に処理された計算でHTMLコードを返すか含んでいますか? –

+0

コンソールにエラーがありますか?このコードを実行すると '$( 'weatherType')'は存在しますか?それはあなたの問題を再現するのに十分なコードを見ずに私たちのためのちょうど2つの未知数です – charlietfl

答えて

0

上のイベントをクリックし再バインドする必要があります。ここで

$("#weatherType").click(function() { 
     var check = $(this).text(); 
     if (tempUnit === check) { 
     $(this).text("F");    
     $("#convertTemperature").text(fahrenheit); 
     } else { 
     $(this).text("C");  
     $("#convertTemperature").text(celcius); 
     } 
    }); 

はプレビューです:摂氏に変換華氏については https://codepen.io/ziruhel/pen/BmVNqJ

<button id='weatherType'>C</button> 
<div id='convertTemperature'>celcius</div> 

そして

$("#weatherType").click(function() { 
    var check = $(this).text(); 
    if (check === "C") { 
    $(this).text("F"); 
    $("#convertTemperature").text("fahrenheit"); 
    } else { 
    $(this).text("C"); 
    $("#convertTemperature").text("celcius"); 
    } 
}); 

は、ライブプレビューを参照してください https://codepen.io/ziruhel/pen/JOZYJW

0

codepenされますが、私はあなたがこれをしたいと思いますクリック

$('#weatherType').click(function() { 
    performConversion(); 
    }); 

function performConversion(){ 
var check = $('#weatherType').text(); 
       if(check === "C"){ 
        $('#weatherType').html("F"); 
        $('#convertTemperature').html(fahrenheit); 
       } 
       else{ 
        $('#weatherType').html("C"); 
        $('#convertTemperature').html(celcius); 
       } 
$('#weatherType').unbind("click"); 
$('#weatherType').click(function() { 
    performConversion(); 
    }) 

} 
+0

なぜ彼はそれをする必要がありますか? – j08691

+0

divのHTMLを置き換えているので、そのdivのclickイベントを再度バインドする必要があります。 –

+0

[mcve]がなければ、何も表示されません。私はあなたを落胆した人ではなかった – j08691

関連する問題