我的.html.erb中的动态Javascript

wil*_*ood 5 javascript ruby-on-rails erb

我在第一个rails应用程序中遇到了一些Javascript.

部分:_care_point.html.erb

<script>
  $(function() {
    $( ".draggable" ).draggable({grid: [50, 20]});
    $( ".node_input").each (function() {
      $(this).hide();
    });
    $("#<%="node.#{care_point.id}" %>").live('dblclick', function(){
      console.log('moo');
      jQuery(this).hide();
      jQuery('.node_input', jQuery(this).parent()).show();
    });
  });
</script>
<div id=<%="care_point.#{care_point.id}" %> class='draggable node_chin'>
  <div id=<%="node.#{care_point.id}" %> class='node'><%= care_point.body %>
  </div>
  <textarea class='node_input'><%= care_point.body %></textarea>
</div>
Run Code Online (Sandbox Code Playgroud)

这是输出:

    <script> 
  $(function() {
    $( ".draggable" ).draggable({grid: [50, 20]});
    $( ".node_input").each (function() {
      $(this).hide();
    });
    $("#node.1").live('dblclick', function(){
      console.log('moo');
      jQuery(this).hide();
      jQuery('.node_input', jQuery(this).parent()).show();
    });
  });
</script> 
<div id=care_point.1 class='draggable node_chin'> 
  <div id=node.1 class='node'>Moo foo
  </div> 
  <textarea class='node_input'>Moo foo</textarea> 
</div>
Run Code Online (Sandbox Code Playgroud)

我首先添加了基于类的dblclick事件监听器,但这导致它被多次添加.这使我将其更改为基于id的方法,但现在它不起作用.是因为我试图动态建立id吗?

这甚至是做这种事情的最好的地方吗?

Nic*_*tti 2

问题就在这里:

 $("#'#node.2'").live('dblclick', function(){
Run Code Online (Sandbox Code Playgroud)

要工作它必须是:

 $('#node.2').live('dblclick', function(){
Run Code Online (Sandbox Code Playgroud)

我不是红宝石专家,但你必须在这里改变一些东西:

 $(<%="'#node.#{care_point.id}'" %>).dblclick(function(){
Run Code Online (Sandbox Code Playgroud)

我会尝试(但我猜 - 已编辑)

$('#<%=node.#{care_point.id} %>').dblclick(function(){
Run Code Online (Sandbox Code Playgroud)

编辑 - 尝试删除 HTML id 中的点:看看这个小提琴http://jsfiddle.net/JeHuD/

代替

#<%=node.#{care_point.id} %>
Run Code Online (Sandbox Code Playgroud)

with (在 jquery 选择器和 HTML 中(还要考虑 html 中的 id 应该有双引号,如下所示: id="node1")

#<%=node#{care_point.id} %>
Run Code Online (Sandbox Code Playgroud)

最终编辑 - 在 jquery 选择器中,您必须使用双反斜杠转义点:这里是与点一起使用的更新的小提琴http://jsfiddle.net/JeHuD/1/