2013-04-29 47 views
0

我現在有一個ajax按鈕。如何將MouseOver操作添加到此視圖?

<% 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 %> 

當我將鼠標光標放在顯示「Now Following」的按鈕上時,我想顯示它。

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

如何將這個應用到我的視圖與MouseOver條件? (只有當它顯示「現在下面的」)

回答

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中寫,所以希望上面是正確的:)

+0

我想知道如何用你的第二個建議來做到這一點。請告訴我如何與那一個:) – cat 2013-04-30 00:42:22

+0

更新我的答案。 – 2013-04-30 00:54:48

+0

你的代碼不會顯示':class =>'btn btn-danger'',這會使它變成紅色。它應該在哪裏? – cat 2013-04-30 01:01:23