我试图做到这一点,当用户点击字母时,它会改变天气类型并转换温度。例如,当我单击#weatherType 时,我希望它更改为华氏度,而当我想返回摄氏度时,我将再次单击#weatherType 但是当我单击它时,它什么也不做。我究竟做错了什么?
编辑:这是我的代码笔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);
}
});
我想你想要这个。
<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/BmVNqJ
将华氏温度转换为摄氏温度:
$("#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/JOZYJW
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句