-1
我正在使用jQuery編寫下面的代碼。我的代碼找到用戶的地理位置和處理錯誤,我已經使用多個if else語句jQuery Mutiple如果其他語句
<script>
$(function() {
var error = $('#errDiv');
$('#btnFindLoc').click(function() {
if (Modernizr.geolocation) {
navigator.geolocation.getCurrentPosition(currentPosition, positionError);
}
else {
error.html("GeoLocation API of HTML 5 is not supported");
}
});
function currentPosition(currentPos) {
var coordinates = currentPos.coords;
$('#lati').text(coordinates.latitude);
$('#longi').text(coordinates.longitude);
var googleMap = $('#gMap');
googleMap.attr("href", "http://maps.google.com/maps?q=" + coordinates.latitude + "," + coordinates.longitude);
}
function positionError(errCode) {
if (errCode.code==0) {
error.html("Unknown Error - has occured");
}
else if (errCode.code==1) {
error.html("Permission Denied - By the user");
}
else if (errCode.code==2) {
error.html("Position/Location Unavailable");
}
else if (errCode.code == 3) {
error.html("Timeout");
}
}
});
</script>
有沒有更好的方式來寫這整個代碼或者是這段代碼好嗎?
使用switch語句。 – brenners1302
你到底在找什麼?你可以用多種方式編寫這個功能,如果他們給你正確的結果,所有這些功能都可以「好」。 – mjuarez
..但是使用'if else'語句沒有真正的問題 – Neilos