Javascript 从Rails链接到调用jQuery函数
我有一个ruby循环,可以创建注释列表 我想知道在这种情况下是否可以将jQuery函数附加到Rails link_to helperJavascript 从Rails链接到调用jQuery函数,javascript,jquery,ruby-on-rails,link-to,Javascript,Jquery,Ruby On Rails,Link To,我有一个ruby循环,可以创建注释列表 我想知道在这种情况下是否可以将jQuery函数附加到Rails link_to helper <% @video.phrases.each do |phrase| %> <div class = "span4" id ="comment" ><%= phrase.content %></div><a id ="ff" ><%= image_tag("ff.png", :s
<% @video.phrases.each do |phrase| %>
<div class = "span4" id ="comment" ><%= phrase.content %></div><a id ="ff" ><%= image_tag("ff.png", :size=> "32x32" )%></a>
<% end %>
“32x32”)%>
我希望像这样的事情
<% @video.phrases.each do |phrase| %>
<div class = "span4" id ="comment" ><%= phrase.content %></div><%= link_to (image_tag("ff.png", :size=> "32x32" ), html => {<script>$("#video_div").html('CONTENTS OF HTML');</script>} :remote => true %>
<% end %>
“32x32”),html=>{$(“#video_div”).html('CONTENTS OF html');}:remote=>true%>
我知道它不起作用,但我想知道有没有一种简单的方法来实现这种功能
谢谢 这有两种方法 第一个是在链接_上添加html属性,以:
<% @video.phrases.each do |phrase| %>
<div class = "span4" id ="comment" >
<%= phrase.content %>
</div>
<%= link_to (image_tag("ff.png", :size=> "32x32" ), html => {:onclick => "$('#video_div').html('CONTENTS OF HTML');"} :remote => true %>
<% end %>
“32x32”),html=>{:onclick=>“$('#video_div').html('CONTENTS OF html');”}:remote=>true%>
第二个是将Javascript与Ruby分开:
<% @video.phrases.each do |phrase| %>
<div class = "span4" id ="comment" >
<%= phrase.content %>
</div>
<%= link_to (image_tag("ff.png", :size=> "32x32" ) :remote => true %>
<% end %>
<script type="text/javascript">
$('a').click(function(){
$("#video_div").html('CONTENTS OF HTML');
);
</script>
“32x32”):远程=>true%>
$('a')。单击(函数(){
$(“#video_div”).html('CONTENTS OF html');
);
如果需要链接标记的内容,请将
'contents of HTML'
替换为$(this).HTML()
您可能需要使用事件委派,因为您似乎将创建大量注释
因此,借用格兰特的话,比如:
<% @video.phrases.each do |phrase| %>
<div class = "span4" id ="comment" >
<%= phrase.content %>
</div>
<%= link_to (image_tag("ff.png", :size=> "32x32" ) :remote => true %>
<% end %>
“32x32”):远程=>true%>
$(“#此处注释包装器名称”)。在(“单击”,“a”,函数(){
$(“#video_div”).html('CONTENTS OF html');
);
此外,我还发现了Rails link\u to\u函数助手,并能够通过以下方式实现所需的行为:
<% @video.phrases.each do |phrase| %>
<div class = "span4" id ="comment" ><%= phrase.content %></div><a id ="ff">
<%= link_to_function image_tag("ff.png", :size=> "32x32" ), "use_comment('#{phrase.comment}')"%>
</a>
“32x32”),“使用注释('.{phrase.comment}')”%>