我有一个显示和隐藏div显示和隐藏点击时的评论。默认情况下,在它被点击之前,它显示消息计数加上一些向下箭头,例如如何在Rails ruby中访问rails helper和嵌入式ruby资产JavaScript文件?
“55条[向下箭头]”
,并单击时更改为:
“隐藏评论[向上箭头]”
我使用此代码显示默认的评论数量和箭头(用户/新):
<span class="commentsCount"><%= pluralize(m.comments.count, 'comment') %></span>
<span class="commentArrows"><%= image_tag 'view_all_comments.png' %> </span>
我使用jQuery来帮助实现改变(用户/ load_comments.js.erb):
$('.postHolder').off().on('ajax:success', '.view_all_comments', function(){
var postContent = $(this).parents('.post_content'),
commentContainer = postContent.find('.comment_container'),
getComments = "<%= j render 'users/partials/show_all_comments' %>";
commentContainer.slice(0, -2).remove();
// The next 2 lines replace the comment count with "Hide comments" and up arrows"
postContent.find('.commentsCount').html("Hide comments");
postContent.find(".commentArrows img").prop("src", "/assets/up_arrows.png");
postContent.find('.comment_container:first').before(getComments);
});
现在重要的部分是当隐藏评论再次点击。我需要它恢复显示评论数量。我可以将箭头还原为原始箭头,但文本中我没有看到回到注释帐户的方式,因为我无法在assets/javascripts/users.js文件中访问rails helper和实例变量等。
下面是代码(资产/ Java脚本/ users.js:
// for showing and hiding comments
$('.expandComments').click(function() {
var showAllCommentsSubmit = $(this).find('.showAllCommentsSubmit'),
postContent = $(this).parents('.post_content'),
commentContainer = postContent.find('.comment_container');
if ($(this).hasClass('visible')) {
postContent.find(".comment_container").slice(0, -1).hide();
postContent.find(".commentArrows img").prop("src", "/assets/view_all_comments.png");
//Here is where I need the code to replace the "Hide comments" text.
} else {
if (showAllCommentsSubmit.is(':disabled')) {
postContent.find(".comment_container").show();
postContent.find(".commentArrows img").prop("src", "/assets/hide_comments.png");
} else {
showAllCommentsSubmit.trigger('submit');
showAllCommentsSubmit.prop({disabled: true});
postContent.find(".commentArrows img").prop("src", "/assets/comments-loader.gif");
}
}
$(this).toggleClass('visible');
});
因为我不认为这是可以使用嵌入的Ruby/Rails佣工JavaScript文件我怎么会是资产能够达到我想要达到的。
是否有某种方式来显示其他HTML基础上增加一些HTML然后将其删除时,不需要的话更多,并有HTML下方再次显露呢?
我敢肯定还有其他一些解决方案,我可以拿出来,比如带来一些css进入游戏..有一个div隐藏和显示,但我还没有想到我会如何做到这一点。想知道是否有快速解决方案。