1

我是 Coffeescript 的新手,并且在语法上苦苦挣扎。谁能帮助我如何用 CS 编写以下内容?

$("#getLocation").click(function() {
  $('#location-loading').show();
  navigator.geolocation.getCurrentPosition(applyLocation);
  return false;
});

function applyLocation(location) {
  $('#LogLongitude').val(location.coords.longitude);
  $('#LogLatitude').val(location.coords.latitude);
  alert('Latitude:' + location.coords.latitude + ', Longitude: ' + location.coords.longitude + ', Accuracy: ' + location.coords.accuracy);
  $('#location-loading').hide();
}

我认为以下内容会起作用,但我在调用函数并返回 false 时遇到错误(所以我不关注链接)。

$('#getLocation').click ->
  $('#location-loading').show()
  navigator.geolocation.getCurrentPosition(applyLocation)
  false

applyLocation = (location) ->
  $('#LogLongitude').val(location.coords.longitude)
  $('#LogLatitude').val(location.coords.latitude)
  alert('Latitude:' + location.coords.latitude + ', Longitude: ' + location.coords.longitude + ', Accuracy: ' + location.coords.accuracy)
  $('#location-loading').hide()
4

1 回答 1

1

您可以省略()简单函数调用(未链接)的括号,并将下部的字符串放入双引号中以便能够使用#{}语法,但除了您的代码看起来已经很咖啡色了;)

$('#getLocation').click ->
  $('#location-loading').show()
  navigator.geolocation.getCurrentPosition applyLocation
  false

applyLocation = (location) ->
  coords = location.coords
  $('#LogLongitude').val coords.longitude
  $('#LogLatitude').val coords.latitude
  alert "Latitude: #{coords.latitude}, 
         Longitude: #{coords.longitude}, 
         Accuracy: #{coords.accuracy}"
  $('#location-loading').hide()
于 2012-05-21T19:52:54.570 回答