我的 Rails 4 应用程序根本不处理 jQuery .click 事件。
在 Chrome 浏览器控制台中,当我键入此内容然后单击 h3 时,它就会起作用。
$("h3").click(function() { return alert("Clicked that thang");}); // This alerts on click of a h3
但是当我在我的应用程序中包含等效代码(在 CoffeeScript 中)时,它什么也不做..
# app/assets/javascripts/collaborators.js.coffee
alert("does this work?") // yes this pops up an alert
$("h3").click ->
alert("Does this work?") // sadly nothing now happens when I click my h3's
我可以看出 jQuery 已加载。
Chrome 控制台:
>> $.fn.jquery
"1.10.2"
我还尝试过清理 Assets 管道:
$> 清理 Assets # 但这对我也没有帮助。
救命,怎么了,我错过了什么?
Rails 4.0 应用程序。
最佳答案
试试这个
$("body").on "click", "h3", ()->
alert("Does this work?")
如果情况并非如此,那么 turbolinks
可能是罪魁祸首,请执行此操作
ready = ()->
$("body").on "click", "h3", ()->
alert("Does this work?")
$(document).ready(ready)
$(document).on('page:load', ready)
关于javascript - Rails 没有处理我的 jQuery .click 事件(甚至无法弹出警报。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27310422/