[英]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
為@paintIt
和window.paintIt
無濟於事。 我有一個臨時解決方案,只是在app / assets / javascripts / applications.js中使用普通的舊javascript,但是我想開始使用coffeescript。 有什么建議么?
您需要將
paintIt
添加到全局名稱空間。 參見此處,了解有關如何操作的詳細說明: stackoverflow.com/questions/4214731/
通過@vee。 在此處復制,因此可以關閉此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.