我想切換一個div,以便點擊時它會將溫度轉換爲華氏度,當再次單擊它時,它將恢復到最初由api拉入的攝氏溫度讀數。切換div中包含的HTML
時,我只處理與數字代碼工作:
var fahrenheit = (data.main.temp * 1.8 + 32).toFixed(2);
var celsius = data.main.temp;
$("#temp").on("click", function() {
$("#temp").html($("#temp").html() == fahrenheit ? celsius : fahrenheit)
});
但是當我嘗試添加文本到HTML它不工作,就會切換一次華氏轉換,但不再次點擊時切換回來。
var fahrenheit = (data.main.temp * 1.8 + 32).toFixed(2);
var celsius = data.main.temp;
$("#temp").on("click", function() {
$("#temp").html($("#temp").html() == fahrenheit + "° F" ? celsius + "° C" : fahrenheit + "° F")
});
我對此有點難住。我搜索了一下,但似乎無法找到適合我想要的解決方案。
感謝您的任何幫助。
這裏是我的codepen
所以'#temp'是...?請包括標記。 – 82Tuskers
對不起,新手使用本網站#temp是一個元素的ID。我將編輯我的原始文章,以包含指向codepen的鏈接。感謝您的高舉。 – Seekingcat
我的猜測是它可能與'&deg'有關。當把它放在html中時,它轉向'°',這將如何比較? –