试图自学如何在 Rails 中使用 js,我发现了一个关于该主题的好视频,但不知何故它对我不起作用,我也不知道为什么。
app/views/static_pages/help.html.erb:
...
<p><%= link_to "Description", "#", id: "desc-link" %></p>
...
应用程序/javascripts/static_pages.coffee:
$(document).on "page:change", ->
$("#desc-link").click ->
alert "Done!"
如果我点击链接,没有任何反应(也没有错误)。
最佳答案
在 rails 5 中。
我在添加 require jquery 后重启了服务器
我的工作代码
$(document).on('turbolinks:load', ->
$("#desc-link").click ->
alert("Done!")
)
或
$(document).on('turbolinks:load', ->
$("#desc-link").on 'click', ->
alert("Done!")
)
关于javascript - 基本的javascript;点击提醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39305367/