【发布时间】:2016-12-02 17:39:50
【问题描述】:
点击前:
点击后:
$(document).ready(function () {
var output = document.getElementById("whole");
if (!navigator.geolocation) {
$("#whole").html("<p>Your brower is not supported</p>");
return;
}
function success(position) {
var lan = position.coords.latitude;
var lon = position.coords.longitude;
$.ajax({
url: "http://api.openweathermap.org/data/2.5/weather?lat=" + lan + "&lon=" + lon + "&APPID=f06e25b1205b65171ad01524870cbb01",
success: function (data) {
$("#temp").text(Math.round(data.main.temp - 273.15));
}
});
}
function error() {
output.innerHTML = "Unable to retrieve your location";
}
navigator.geolocation.getCurrentPosition(success, error);
var ce = parseInt($("#temp").text(), 10);
var fa = Math.round(ce * 1.8 + 32);
$("a").on("click", function () {
if ($(this).text() == "℃") {
$(this).html("℉");
$("#temp").text(fa);
} else {
$(this).html("℃");
$("#temp").text(ce);
}
});
});
<script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<span id="temp"></span><a id="toggle" href="javascript:void(0)">℃</a>
我想实现这个,当我点击摄氏度字符时,摄氏度会变成华氏度。因为代码需要位置,所以运行sn -p会报错。不知道为什么是 NaN。但是没有用。为什么?
【问题讨论】:
标签: javascript jquery