是否可以从记录字段填充 javascript 属性?
我有一个场地记录表,这些记录作为部分内容显示在索引页上。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这两方面都是的。您现在遇到的问题是,您在 javascript 中通过 ID 引用元素,但该 ID 不是唯一的,因此 javascript 找到该 ID 的第一个实例并将规则应用于它,而不是应用于其余的 div。要解决此问题,您需要使用唯一的 id:
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:
当然,它只需要像在 HTML 中一样输出即可:
Sure, it just needs to be output just like you do in the HTML: