なんらかの理由で、クリックイベントが温度要素(id:#temp)で1回発生することがあります。それが華氏に変換された後、それは摂氏に戻らない。私はjQueryを使い始めたので、どんな助けでも大歓迎です。ソースコードは以下の通りです。jQueryイベントトリガを1回だけ実行する
HTMLソース:
<html>
<head>
<link rel="stylesheet" href="main.css"/>
<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
<script src="main.js"></script>
<script type="text/javascript" async src="https://platform.twitter.com/widgets.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"></link>
</head>
<body>
<h1>Local Weather Code Camp Project</h1>
<h4>Location</h4>
<p id="location"></p>
<h4>Weather</h4>
<p id="weather"></p>
<img id="icon">
<h3 id="temp"></h3>
<p id="check"></p>
</body>
</html>
jQueryの出典:
$(document).ready(function() {
navigator.geolocation.getCurrentPosition(function(position) {
$.getJSON('https://fcc-weather-api.glitch.me/api/current?lat=' + position.coords.latitude + '&lon=' + position.coords.longitude , function(data){
var tmpInt = 0;
$("h1").html(JSON.stringify(data));
$("#location").html(data.name);
$("#weather").html(data.weather[0].description);
$("#icon").attr("src", data.weather[0].icon);
$("#temp").html(data.main.temp + "° C");
$(document).on('click','#temp',function() {
tmpInt = data.main.temp;
tmpInt = tmpInt * 1.8 +32;
if("#temp:contains('° C')") {
$("#temp").html(tmpInt + "° F");
$("#check").html("Contains C");
}
else if("#temp:contains('° F')") {
$("#temp").html(data.main.temp + "° C");
$("#check").html("Contains F");
}
});
});
});
});
ワーキングCodePen here。
注意深く** if( "#temp:contains( '° C')") 'で。評価していると思いますか? – Phil
@Philのコメントに加えて。クリックイベントが発生し、関数が呼び出されています。私はあなたのCodePenの例を試しました。ちょうど単純な 'alert'を書いています。だからあなたの問題はあなたが述べたものではありません。 –
@Phil私はそれが与えられた要素にその文字列が含まれているかどうかを評価すると思うでしょうし、そうでない場合は次のifまたはexitに移動しますか? –