Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将ID放在HAML线的中间_Javascript_Ruby On Rails_Haml - Fatal编程技术网

Javascript 将ID放在HAML线的中间

Javascript 将ID放在HAML线的中间,javascript,ruby-on-rails,haml,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标记,

我想要实现的是

显示为

Comments (32)
其中32是针对帖子的评论计数。现在,这在haml中很容易实现

%h2 Comments (#{Post.count_of_comments})
但是,我正在使用AJAX和javascript创建和删除注释。我需要能够根据计数放置一个标识符,以便在javascript中调整它。显然,我能做到

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

但是,有没有办法只针对计数而不是整行设置id标记,或者如果没有,我如何在javascript中识别该计数。

这样做会有效果吗

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

您可以将ID应用于h2本身,如下所示:

%h2#count Comments (#{Post.count_of_comments})
或对计数应用范围:

%h2 Comments
    %span#count (#{Post.count_of_comments})
如果您只想用Javascript插入原始数字,还可以通过CSS添加参数:

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

JS:


使用CSS类标记注释,并使用jQuery对它们进行计数
var numItems=$('.yourclass')。length
是的,对不起,我实际上是在问如何使用类进行标记
#count:before {
    content: "(";
}

#count:after {
    content: ")";
}
var newCount = 42;
$('#count').html(newCount);