繁体   English   中英

Coffeescript未捕获参考

[英]Coffeescript Uncaught Reference

我正在遵循Rails指南的第一步,网址是http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#unobtrusive-javascript

index.html.erb看起来像<a href="#" onclick="paintIt(this, '#990000')">Paint it red</a> <a href="#" onclick="paintIt(this, '#009900', '#FFFFFF')">Paint it green</a> <a href="#" onclick="paintIt(this, '#000099', '#FFFFFF')">Paint it blue</a>

我在app / assets / javascripts / welcome.js.coffee paintIt = (element, backgroundColor, textColor) -> element.style.backgroundColor = backgroundColor if textColor? element.style.color = textColor paintIt = (element, backgroundColor, textColor) -> element.style.backgroundColor = backgroundColor if textColor? element.style.color = textColor

我收到此错误:
Uncaught ReferenceError: paintIt is not defined

我尝试将@paintIt paintIt@paintItwindow.paintIt无济于事。 我有一个临时解决方案,只是在app / assets / javascripts / applications.js中使用普通的旧javascript,但是我想开始使用coffeescript。 有什么建议么?

您需要将paintIt添加到全局名称空间。 参见此处,了解有关如何操作的详细说明: stackoverflow.com/questions/4214731/

通过@vee。 在此处复制,因此可以关闭此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM