将一个id放在haml行的中间

时间:2014-08-13 21:33:24

标签: javascript ruby-on-rails haml

我想要实现的是......

显示为

的行
Comments (32)

其中32是针对帖子的评论计数。现在用haml

很容易实现
%h2 Comments (#{Post.count_of_comments})

但是,我正在使用AJAX和javascript创建和删除评论。我需要能够为计数放置一个标识符,以便我可以在javascript中调整它。显然,我可以做到

#count
  %h2 Comments (#{Post.count_of_comments})

但是有没有办法将id标记放在计数而不是整行上,或者没有说明如何在javascript中识别该计数。

2 个答案:

答案 0 :(得分:2)

你可以像这样对h2本身应用一个ID:

%h2#count Comments (#{Post.count_of_comments})

或者对计数应用跨度:

%h2 Comments
    %span#count (#{Post.count_of_comments})

如果您只是想用Javascript插入原始数字,也可以通过CSS添加parens:

%h2 Comments
    %span#count #{Post.count_of_comments}

CSS:

#count:before {
    content: "(";
}

#count:after {
    content: ")";
}

JS:

var newCount = 42;
$('#count').html(newCount);

答案 1 :(得分:0)

这样的事情可以解决吗?

%h2
  Comments
  = surround '(', ')' do
    %span#custom-id
      = Post.count_of_comments