简体   繁体   English

将动态生成的div id从Rails传递给javascript / coffeescript

[英]Pass dynamically generated div id's from Rails to javascript/coffeescript

I have a list of items for which I would like to reveal a more detailed description per item under the name of the item at the click of a button or link. 我有一个项目列表,我想通过点击按钮或链接在项目名称下显示每个项目的更详细说明。 For example, a very simple implementation would be using .toggle(). 例如,一个非常简单的实现将使用.toggle()。 In Rails I have this for the descriptions: 在Rails中我有这个描述:

<% @products.each do |product| %>
    <%= div_for(product, :class => "descriptions") do %>
        <%= product.description %>
    <% end %>
    <input type="button" id="button_<%= product.id %>" value="Show/hide" />
<% end %>

I would have to catch the dynamically generated div id's at the javascript/coffeescript side: 我必须在javascript / coffeescript端捕获动态生成的div id:

jQuery ->
$('#button_?????').click ->
    $('#product_ ????').toggle()

However, I'm unable to get this to work. 但是,我无法让它工作。

On the other hand, I might be able to accomplish the same result using something like '.closets()', like this: 另一方面,我可以使用像'.closets()'这样的结果来完成相同的结果,如下所示:

jQuery ->
$('#button').click ->
    $('#button').closest('.descriptions').toggle()

However, I can't get this to work either. 但是,我也无法让这个工作。 It seems to be a fairly simple task, but I'm just too unexperienced in javascript to get this to work. 这似乎是一个相当简单的任务,但我只是在javascript中没有经验,无法使其工作。 How to catch these dynamically generated divs on the javascript side? 如何在javascript端捕获这些动态生成的div? Any help would be very much appreciated. 任何帮助将非常感谢。

If only DIVs you care about in this case will have a class of "descriptions", you can use jQuery's live function . 如果在这种情况下只有你关心的DIV会有一类“描述”,你可以使用jQuery的实时功能 It would look like this: 它看起来像这样:

$('.descriptions').live("click", function() { ... });

So whenever a new item with the descriptions class is formed, it will gain that jQuery code for its click event. 因此,每当形成具有描述类的新项时,它将获得其click事件的jQuery代码。

In order to capture the dynamically generated div 's from Rails, you would normally do the following: 为了从Rails捕获动态生成的div ,通常会执行以下操作:

var divs = document.querySelectorAll('.descriptions');

Keep in mind, though, that querySelectorAll is NOT live, therefore any changes to the DOM will not be reflected in the NodeList . 但请记住, querySelectorAll不是活动的,因此对DOM的任何更改都不会反映在NodeList

jQuery has a great solution for this, as @MrDanA had suggested. 正如@MrDanA建议的那样,jQuery有一个很好的解决方案。 However, the click handler needs to be on the button and not the description. 但是, click处理程序需要位于按钮上,而不是描述。 Also, live has been deprecated. 此外, live已被弃用。 It needs to be replaced with on . 它需要替换为on

$('.descriptions').each(function(){
  var $this = $(this);
  var button = $this.next(); // assuming that the button is the next sibling
  button.on('click', function(e) {
    $this.toggle();
    e.preventDefault(); // to prevent page from jumping to the top of the page
  });
});

This should give you a good baseline. 这应该给你一个很好的基线。 Let me know exactly how you'd like it to behave. 让我确切地知道你喜欢它的表现方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM