How to make a newly posted comment the selector in jquery (with rails)?
I am trying to highlight the new message @micropost when it is posted using the second line of this code but am not having any success:
$("table.microposts").prepend("<%= escape_javascript(render(:partial => 'shared/feed_item', :object => @micropost)) %>");
$("#table.micropost_<%= @micropost.id %>").effect("highlight", {}, 3000);
$("#new_micropost")[0].reset();
This is the feed item partial (I tried using the div_for function to identify the new post...:
<tr> <%= div_for fee开发者_如何学编程d_item do %>
<td class="gravatar">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
</td>
<td class="micropost">
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content">
<%= feed_item.content %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
</td>
<% if current_user?(feed_item.user)%>
<td>
<%= link_to "delete", feed_item, :method => :delete,
:confirm => "You sure?",
:title => feed_item.content %>
</td>
<% end %>
<% end %>
</tr>
<% if @feed_items.any? %>
<table class="microposts" summary="Status feed">
<%= render :partial => 'shared/feed_item', :collection => @feed_items %>
</table>
<%= will_paginate @feed_items %>
<% end %>
Hope that gives sufficient detail. Thanks for any help
You can use prependTo instead of prepend and then do it like:
$("<%= escape_javascript(render(:partial => 'shared/feed_item', :object => @micropost)) %>").
prependTo($("table.microposts")).
effect("highlight", {}, 3000);
You will need the .live() function, if you add something after the page was loaded (i.e with prepend).
Have a look here: http://api.jquery.com/live/
精彩评论