为什么我的coffescript函数没有执行

Why my coffeescript function is not executed?

本文关键字:执行 函数 我的 coffescript 为什么      更新时间:2023-09-26

我对此非常陌生,所以我正在努力遵循http://guides.rubyonrails.org/working_with_javascript_in_rails.html以及http://guides.rubyonrails.org/asset_pipeline.html导轨导轨(我用的是Rails4)

所以在views/myController中我有

<a href="#" onclick="paintIt(this, '#990000')">Paint it red</a>

app/assets/javascripts/myController.js.coffee中的paintIt函数

paintIt = (element, backgroundColor, textColor) ->
  element.style.backgroundColor = backgroundColor
  if textColor?
   element.style.color = textColor

在app/assets/javascripts/application.js中,我有一个指令:

//= require_tree .

因此myController.js.coffee中的coffescript应该编译为

在视图/布局/应用程序中,我有

javascript_include_tag "application"

我还检查了咖啡轨道gem是否在gemfile中并已安装。

然而,在我看来,我看到了"把它涂成红色"链接,但coffeescript功能没有被触发。(什么都没发生)

还检查了如果我在内联中编写javascript,它是否能正常工作

这是什么?我错过了什么?

如注释中所述,coffee脚本与顶级函数包装器一起使用。如果手动编译文件,则可以使用--bare命令行选项。

否则,我将向您介绍以下帖子:我如何使用选项"--裸露的";CoffeeScript的Rails 3.1中?