我的.html.erb中的动态Javascript

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

部分:_care_point.html.erb

 $(function() { $( ".draggable" ).draggable({grid: [50, 20]}); $( ".node_input").each (function() { $(this).hide(); }); $("#").live('dblclick', function(){ console.log('moo'); jQuery(this).hide(); jQuery('.node_input', jQuery(this).parent()).show(); }); });  <div id= class='draggable node_chin'> <div id= class='node'> 

这是输出:

   $(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(); }); });  
Moo foo

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

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

问题出在这里:

  $("#'#node.2'").live('dblclick', function(){ 

要工作,必须是:

  $('#node.2').live('dblclick', function(){ 

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

  $(<%="'#node.#{care_point.id}'" %>).dblclick(function(){ 

我试试(但我猜 – 编辑)

 $('#<%=node.#{care_point.id} %>').dblclick(function(){ 

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

更换

 #<%=node.#{care_point.id} %> 

with(在jquery选择器和HTML中都有(也考虑到你的html中的id应该有这样的双引号:id =“node1”)

 #<%=node#{care_point.id} %> 

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

请尝试以下方法:

 $("#<%="node.#{care_point.id}" %>").live('dblclick', function(){ console.log('moo'); jQuery(this).hide(); jQuery('.node_input', jQuery(this).parent()).show(); }); 
Interesting Posts