javascript 从 Rails link_to 调用 jQuery 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10134639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:48:23  来源:igfitidea点击:

Call jQuery function from Rails link_to

javascriptjqueryruby-on-railslink-to

提问by Stpn

I have a ruby loop that creates a list of comments..

我有一个创建评论列表的 ruby​​ 循环。

I wonder if I can attach jQuery function to Rails link_to helper in this case?

我想知道在这种情况下我是否可以将 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", :size=> "32x32" )%></a>
    <% end %>

I am hoping for something like

我希望有类似的东西

    <% @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 %>

I know it won't work, but I wonder is there an easy way to achieve this kind of functionality?

我知道这行不通,但我想知道是否有一种简单的方法可以实现这种功能?

Thanks!

谢谢!

采纳答案by grant

You can do this two ways.

你可以通过两种方式做到这一点。

The first is the add an html attribute on the link_to:

首先是在link_to上添加一个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 %>

The second is to separate the Javascript from Ruby:

第二个是将 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>

If you want the contents of the link tag, substitute 'CONTENTS OF HTML'for $(this).html()

如果您想要链接标签的内容,请替换'CONTENTS OF HTML'$(this).html()

回答by Stpn

Also I actually found out about Rails link_to_function helper and was able to achieve the desired behavior with:

此外,我实际上发现了 Rails link_to_function helper 并能够通过以下方式实现所需的行为:

<% @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>

回答by Neil

You might want to use event delegation, since it seems like you'll be creating a large number of comments.

您可能想要使用事件委托,因为您似乎要创建大量评论。

So, borrowing from grant something like:

因此,从 Grant 中借用类似的东西:

<% @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">
  $("#comment-wrapper-name-here").on("click", "a", function() {
    $("#video_div").html('CONTENTS OF HTML');
  );
</script>