jQueryを追加したスパン要素をクリック可能にするためにCを作成しようとしていますが、クリックしたときにFに変更したいのですが、何らかの理由で機能しません。jQueryクリック可能な文字を追加する
$(document).ready(function(){
$("#location").html("<b>New York, NY</b>");
$("#temperature").html("112");
$("#temperature").append("<span> C </span>");
$("span:contains('C')").attr("id", "type");
$("span:contains('C')").click(changeNotation());
});
function changeNotation(){
var notation = document.getElementById("type");
if(notation.textContent == 'C')
notation.innerHTML = 'F';
else if(notation.textContent == 'F')
notation.innerHTML = 'C';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
<div class="row text-center well">
<h2 class="text-info"> Welcome to the <span><b>Weather App!</span></b></h2>
<p class="text-primary bg-info text-center">
This app will display the weather based on your current location. Click on the C or F to toggle between celcius and fahrenheit.</p>
<h2 class="text-center text-info"><b> Current location </b></h2>
<h1 class="text-center text-info" id="location"> </h1>
<h2 class="text-center text-info"> <b>Current Temperature</b> </h2>
<h1 class="text-center text-info" id='temperature'> </h1>
</div>
</div>
別のクリックイベントが必要になりますが含まれています。私は約天気アプリを教えている! – Masiama
jQueryでonclickイベントを追加するには、 '$(" span:contains( 'C') ")をon( 'click'、changeNotation); ' – Masiama
'に追加し、 'span'に 'C'を追加してください。 – Masiama