我的.html.erb中的动态Javascript [英] Dynamic Javascript in my .html.erb
问题描述
我在第一个rails应用程序中遇到了一些Javascript。
I'm struggling with some Javascript in my first rails application.
部分:_care_point.html.erb
Partial: _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>
这是输出:
<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>
我首先添加了基于类的dblclick事件监听器,但这导致它被多次添加。这使我将其更改为基于id的方法,但现在它不起作用。是因为我试图动态建立id吗?
I first added the dblclick event listener classbased, but that caused it to be added multiple times. This made me change it to a id based approach, but now it does not work. Is it because I try to dynamically build up the id?
这甚至是做这种事情的好地方吗?
Is this even the righ place to do this kind of thing?
推荐答案
问题在于:
$("#'#node.2'").live('dblclick', function(){
要工作必须be:
$('#node.2').live('dblclick', function(){
我不是红宝石的专家,但你必须在这里改变一些东西:
i'm no expert of ruby but you must change something here:
$(<%="'#node.#{care_point.id}'" %>).dblclick(function(){
我试试(但我猜 - 编辑)
I'd try (but i'm guessing - edited)
$('#<%=node.#{care_point.id} %>').dblclick(function(){
编辑 - 尝试删除HTML的ID中的点:看看这个小提琴 http://jsfiddle.net/JeHuD/
EDIT - Try removing the dots in the ids of th HTML: look at this fiddle http://jsfiddle.net/JeHuD/
替换
#<%=node.#{care_point.id} %>
with(在jquery选择器和HTML中都有)(还要考虑你的html中的id应该是有这样的双引号:id =node1)
with (both in the jquery selector and in the HTML (also consider that your id in the html should have double quotes like this: id="node1")
#<%=node#{care_point.id} %>
最终编辑 - 在jquery选择器中你必须使用小数反斜杠转义点:这是更新的小提琴,可以使用点 http://jsfiddle.net/JeHuD/1/
FINAL EDIT - in the jquery selector you must escape dots withdoubole backslashes: here is the updated fiddle that works with the dot http://jsfiddle.net/JeHuD/1/
这篇关于我的.html.erb中的动态Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!