簡體   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