【问题标题】:Write Ajax function in Coffeescript在 Coffeescript 中编写 Ajax 函数
【发布时间】:2015-08-22 06:51:34
【问题描述】:

小提琴 - http://codepen.io/mikethedj4/pen/BNRdVp

今天我决定学习 Coffeescript 并尝试制作一些函数,处理事件等。

但是今天我遇到了一个错误,上面写着“保留字'功能'”,并且还没有想出如何解决它。

原文:

$(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

【问题讨论】:

  • CS中没有function关键字。

标签: javascript jquery ajax coffeescript


【解决方案1】:
$ ->
    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()

【讨论】:

    猜你喜欢
    • 2012-02-21
    • 2011-09-06
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多