开发者

Is it possible to populate a javascript attribute from a records field?

I have a table of venue records which are being displayed on the index page as partials. Is it possible for the .left and .top values in the javascript to be populated by an integer field taken from venue records?

Also, how can I have the script shown run for each partial as it currently only applies to the first partial generated.

Venue partial:

<%= link_to venue do %>
  <div class="venue_partial">

    <div class="venue_icon">
      <%= image_tag venue.venuetype.photo.url(:thumb), :class => 'imag开发者_运维问答e' %>
    </div>

    <span class="venue_partial_name"><%= venue.name %></span>
    <span class="venue_area_and_type"><%= venue.venuetype.name %></span>
    <span class="venue_area_and_type"><%= venue.area.name %></span>
  </div>

  <div id="venue_map_icon" style="position:absolute;"></div>

  <script>
    document.getElementById("venue_map_icon").style.left= "300px";
    document.getElementById("venue_map_icon").style.top= "310px";
  </script>
<% end %>

Thanks very much for any help its much appreciated!


Yes on both counts. The problem you have now is that you are referencing an element by an ID in javascript, but that ID is not unique, so javascript finds the first instance of this ID and applies the rules to it, not to the rest of your divs. To fix this, you need to use unique ids:

<%= link_to venue do %>
  <div class="venue_partial">

    <div class="venue_icon">
      <%= image_tag venue.venuetype.photo.url(:thumb), :class => 'image' %>
    </div>

    <span class="venue_partial_name"><%= venue.name %></span>
    <span class="venue_area_and_type"><%= venue.venuetype.name %></span>
    <span class="venue_area_and_type"><%= venue.area.name %></span>
  </div>

  <div id="venue_map_icon_<%= venue.id %>" style="position:absolute;"></div>

  <script>
    document.getElementById("venue_map_icon_<%= venue.id %>").style.left= "<%= venue.left %>px";
    document.getElementById("venue_map_icon_<%= venue.id %>").style.top= "<%= venue.top %>px";
  </script>
<% end %>


Sure, it just needs to be output just like you do in the HTML:

document.getElementById("venue_map_icon").style.left = "<%= venue.somevalue %>px";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜