我有一个像这样的块:
- competitors.each do |competitor|
%dl
%dt
...
%dd
%span{:id => "#{competitor['watchers']}"}= "#{((competitor['watchers']*100.0)/30000).round.to_f}%"请注意,它会生成动态CSS id,每个块都有一个id,resoulting html是不同dd --> span --> id编号的列表:
<dl>
<dt>
...
<dd>
<span id="774">93.0%</span>
</dd>
</dt>
</dl>
<dl>
<dt>
...
<dd>
<span id="13774">46.0%</span>
</dd>
</dt>
</dl>我想“动态”地将“自定义CSS片段”与不同的css I( #13774 #774 )关联起来,如下所示:
:javascript
$("##{competitor['watchers']}").css({ width: "#{((competitor['watchers']*100)/30000)}px" });没有link_to类型的帮助,我如何调用ajax (在Rails 3.2.3 ':remote => true‘)?
到目前为止,我只是尝试从内部块调用JS,比如:
- competitors.each do |competitor|
:javascript
$("##{competitor['watchers']}").css({ width: "#{((competitor['watchers']*100)/30000)}px" });
%dl
%dt
...
%dd
%span{:id => "#{competitor['watchers']}"}= "#{((competitor['watchers']*100.0)/30000).round.to_f}%"但是它不起作用,代码永远不会被注入到DOM中。
发布于 2012-05-01 19:28:30
看起来您正在尝试显示某种条形图,在这种情况下,我建议使用以下内容(我假设您使用的是jQuery,因为这是标记的内容):
更改您的块,为每个span添加一个唯一的类。这会在以后给你带来风格上的好处。
%dd
%span{:id => "#{competitor['watchers']}", :class => "progress-bar"}= "#{((competitor['watchers']*100.0)/30000).round.to_f}%"然后你应该能够使用页面底部的一些jQuery来选择每一个,并在上面做一些基本的数学运算:
$('span.progress-bar').each(function(index,element){
var num = $(element).attr('id'); // get element id
var width = parseInt(num * 100 / 30000); // do your math, then get the integer value for width
$(element).css('width',width+'px'); // set width
}我知道您正在寻找ajax,但是除非我遗漏了什么,否则这应该可以解决您的文档中所记录的问题。
https://stackoverflow.com/questions/10396933
复制相似问题