是否可以从记录字段填充 javascript 属性?

发布于 2024-10-18 11:56:47 字数 931 浏览 0 评论 0原文

我有一个场地记录表,这些记录作为部分内容显示在索引页上。 javascript 中的 .left 和 .top 值是否可以由从场地记录中获取的整数字段填充?

另外,如何让显示的脚本针对每个部分运行,因为它当前仅适用于生成的第一个部分。

地点部分:

<%= 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" style="position:absolute;"></div>

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

非常感谢您的帮助,非常感谢!

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 => '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" 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤城病女 2024-10-25 11:56:47

这两方面都是的。您现在遇到的问题是,您在 javascript 中通过 ID 引用元素,但该 ID 不是唯一的,因此 javascript 找到该 ID 的第一个实例并将规则应用于它,而不是应用于其余的 div。要解决此问题,您需要使用唯一的 id:

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

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 %>
雅心素梦 2024-10-25 11:56:47

当然,它只需要像在 HTML 中一样输出即可:

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

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

document.getElementById("venue_map_icon").style.left = "<%= venue.somevalue %>px";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文