0

我现在有一个像这样的ajax按钮。

<% if current_user.following?(user) %>
  <%= link_to(unfollow_user_path(user), :remote => true, :class => 'btn') do %>
    Now Following
  <% end %>
<% else %>
  <%= link_to(follow_user_path(user) ,:remote => true, :class => 'btn btn-primary') do %>
    Follow
  <% end %>
<% end %>

当我将鼠标光标移到显示“正在关注”的按钮上时,我想显示这个。

<%= link_to(follow_user_path(user) ,:remote => true, :class => 'btn btn-danger') do %>
  Un-Follow
<% end %>

如何使用 MouseOver 条件将此应用于我的视图?(仅当它显示“正在关注”时)

4

1 回答 1

1

好的,请执行以下操作:

# change the class in your link:
link_to(unfollow_user_path(user), remote: true, class: 'btn now-following') do ... end

# add this code in a file under app/assets/javascripts/
$('.now-following').on({
    mouseover: function() {
        $(this).addClass('btn-danger').text('Un-Follow');
    },
    mouseout: function() {
        $(this).removeClass('btn-danger').text('Now Following');
    }
});

我通常用 CoffeeScript 写,所以希望以上是正确的 :)

于 2013-04-30T00:35:16.023 回答