【问题标题】:coffee script not working咖啡脚本不起作用
【发布时间】:2016-08-04 03:54:35
【问题描述】:

我正在做 Rails 应用程序,我将深入研究 Javascript 以了解我的 Web 应用程序的行为,所以这就是问题所在

我有很多帖子的新闻源,帖子中有很多评论隐藏在帖子中

post.css

#comments-section{
  display: none;
}

发布/索引“新闻提要”

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

<section id="comments-section">

  <%= render 'commenters/newsfeedcomment', obj: post %>

  <% if current_user == post.user %>
    <h6 align="right"><%= link_to 'Edit', edit_post_path(post) %></h6>
    <h6 align="right"><%= link_to 'Delete', { :id => post ,:controller => 'posts',:action => 'destroy'} %></h6>
  <% end %>
</section>

现在我正在尝试检查我的评论链接以触发但它不起作用,我不知道如何调试

$(document).on "page:change", ->
  $('#comments-link').click ->
    alert "clicked"

【问题讨论】:

    标签: javascript ruby-on-rails coffeescript


    【解决方案1】:

    首先,如果您要拥有多个#comments-link,那么您应该将其设为类而不是id。

    其次,您使用 Turbolinks 是否存在问题?有一个 jquery.turbolinks gem 可以让 Turbolinks page:change 事件像 DOM 就绪事件一样工作。

    第三,你也可以把监听器放在一些不进出的DOM元素和DOM上。

    $('.container').on 'click', '#comments-link' ->
      alert 'clicked'
    

    这会将一个侦听器附加到.container 元素,但只侦听来自#comments-link 元素的事件。

    【讨论】:

    • 感谢您的回复,但我不太明白我想我需要再次学习 Javascript 的基础知识,感谢您的回复,我非常感谢
    猜你喜欢
    • 2018-01-25
    • 1970-01-01
    • 2013-02-15
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多