Function Coffeescript函数语法
我对Coffeescript是个新手,还在语法上苦苦挣扎。有人能帮我用CS写以下内容吗Function Coffeescript函数语法,function,coffeescript,Function,Coffeescript,我对Coffeescript是个新手,还在语法上苦苦挣扎。有人能帮我用CS写以下内容吗 $("#getLocation").click(function() { $('#location-loading').show(); navigator.geolocation.getCurrentPosition(applyLocation); return false; }); function applyLocation(location) { $('#LogLongitude').
$("#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时出现了错误(因此我不遵循链接)
对于简单的函数调用(不链接),可以省略()
参数
并将下半部分的字符串放入双配额,以便能够使用#{}
语法,
但除了你的代码看起来已经很像咖啡;)
对于简单的函数调用(不链接),可以省略()
参数
并将下半部分的字符串放入双配额,以便能够使用#{}
语法,
但除了你的代码看起来已经很像咖啡;)
使用js2coffee.org获得更多帮助。结果表明代码运行良好,但我忘了在文件开头包含“jQuery->”,因为我使用jQuery,并且由于复制/粘贴问题,我有一些选项卡而不是空格。使用js2coffee.org获得更多帮助。结果表明代码运行良好,但我忘了包含“jQuery->”在文件的开头,因为我使用jQuery,由于复制/粘贴问题,我有一些选项卡而不是空格。谢谢提示和清理代码。你的代码看起来干净多了。我已经按照你的建议更新了我的建议。谢谢正如我在上面的评论中所提到的,我的代码在将“jQuery->”添加到文件开头后实际上可以工作。感谢您提供的提示和清理代码。你的代码看起来干净多了。我已经按照你的建议更新了我的建议。谢谢正如我在上面的评论中提到的,我的代码实际上是在将“jQuery->”添加到文件开头之后工作的。
$('#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()
$('#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()