在 Coffeescript 中编写 Ajax 函数
Write Ajax function in Coffeescript
Fiddle - http://codepen.io/mikethedj4/pen/BNRdVp
今天我决定学习 Coffeescript 并尝试制作一些函数、处理事件等。
但是今天我收到一个错误 "reserved word 'function'",我还没有想出如何解决它。
原文:
$(function () {
function download_to_textbox(url, el) {
$.get(url, null, function (data) {
el.val(data);
}, "text");
}
download_to_textbox("http://code.jquery.com/jquery-latest.min.js", $("textarea"));
});
我的翻译:
(($) ->
function download_to_textbox(url, el) {
$.get(url, null, (data) ->
el.val(data);
}, "text");
}
download_to_textbox("http://code.jquery.com/jquery-latest.min.js", $("textarea"));
$("textarea").click ->
$(this).select();
) jQuery
$ ->
download_to_textbox = (url, el) ->
$.get url, null, ((data) ->
el.val data
return
), 'text'
download_to_textbox 'http://code.jquery.com/jquery-latest.min.js', $ 'textarea'
$("textarea").click ->
$(this).select()
Fiddle - http://codepen.io/mikethedj4/pen/BNRdVp
今天我决定学习 Coffeescript 并尝试制作一些函数、处理事件等。
但是今天我收到一个错误 "reserved word 'function'",我还没有想出如何解决它。
原文:
$(function () {
function download_to_textbox(url, el) {
$.get(url, null, function (data) {
el.val(data);
}, "text");
}
download_to_textbox("http://code.jquery.com/jquery-latest.min.js", $("textarea"));
});
我的翻译:
(($) ->
function download_to_textbox(url, el) {
$.get(url, null, (data) ->
el.val(data);
}, "text");
}
download_to_textbox("http://code.jquery.com/jquery-latest.min.js", $("textarea"));
$("textarea").click ->
$(this).select();
) jQuery
$ ->
download_to_textbox = (url, el) ->
$.get url, null, ((data) ->
el.val data
return
), 'text'
download_to_textbox 'http://code.jquery.com/jquery-latest.min.js', $ 'textarea'
$("textarea").click ->
$(this).select()