【问题标题】:turbolinks rails 5 toggle event not workingturbolinks rails 5切换事件不起作用
【发布时间】:2017-04-19 20:27:49
【问题描述】:

这是我的咖啡脚本

$(document).on "turbolinks:load", ->
  $('#comments-link').click ->
    $('#comments-section').toggle()

这是我的 .scss 文件

#comments-section{
 display: none;
}

她是 HTML 文件

<%= link_to 'Comments','#', id: "comments-link"%> 

<div id="comments-section">
   <%= render @recipe.comments %>
</div>

当我点击链接时,什么也没有发生。 在 Web 控制台中,代码可以正常工作。 任何建议

【问题讨论】:

    标签: javascript jquery ruby-on-rails turbolinks


    【解决方案1】:

    好吧,我发现我必须阻止Default()

    $('#comments-link').click (e)->
      e.preventDefault()
    

    【讨论】:

      【解决方案2】:

      要让它在 Rails 5 中工作,您需要添加 gem:

      gem 'jquery-rails', '4.3.1'

      然后在“app/assets/javascripts/application.js”中,您需要通过添加以下第一行来要求 jquery(其他 3 行已经在该文件中):

        //= require jquery
        //= require rails-ujs
        //= require turbolinks
        //= require_tree .
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-02
        相关资源
        最近更新 更多