将id放在haml线的中间

place an id in the middle of a haml line

本文关键字:中间 haml id 放在      更新时间:2023-09-26

我想实现的是…

显示为

的行
Comments (32)

其中32是针对一篇文章的评论数。现在,在haml中使用

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

然而,我正在用AJAX和javascript创建和删除注释。我需要能够对计数放置一个标识符,以便我可以在javascript中调整它。显然,我可以写

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

但是是否有任何方法可以将id标签放在计数上而不是整行,或者如果失败,我如何在javascript中识别计数

您可以像这样为h2本身应用ID:

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

或者对count应用span:

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

如果你只是想在Javascript中插入一个原始数字,你也可以通过CSS添加父元素:

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

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

JS:

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

这样的东西会奏效吗?

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