【问题标题】:jQuery not displaying span iteration properlyjQuery没有正确显示跨度迭代
【发布时间】:2013-07-21 22:37:44
【问题描述】:

所以我创建了一个 erb 块来获取每个图像标签的坐标,我想在其中显示每个图像在所述坐标处的标签。但是,只显示一个标签,而不是迭代中的每个标签。知道为什么吗?和.each()有关系吗?

<% if @new_manual.present? %>
<% @new_manual.steps.each do |step| %>
<% i_connection = Contact.find(step.input_contact) %>
<span class="i_connection" data-pos-x="<%= i_connection.pos_x %>" data-pos-y="<%= i_connection.pos_y %>"  data-pos-width="<%= i_connection.pos_width %>" data-pos-height="<%= i_connection.pos_height %>"> </span>  
<br>
<div class='image_panel'>
    <%= image_tag(i_connection.image.image.url(:large)) %>
<div class='planetmap'></div>


<% end %>
<% end %>
</div>

<script type="text/javascript">
$(document).ready(function(){
$("span.i_connection").each(function() {
    var pos_width = $(this).data('pos-width'); 
    var pos_height = $(this).data('pos-height');
    var xpos = $(this).data('pos-x');
    var ypos = $(this).data('pos-y');

    $(".tagged_box").css("display","block");
    $(".tagged").css("border","5px solid red");

    if ((xpos !== undefined) && (ypos !== undefined)) {
    console.log('X:' + xpos + 'px' + ' ' + 'Y:' + ypos +'px');         
        $('.planetmap').append('<div class="tagged"  style="width:'+pos_width+'px;height:'+
        pos_height+'px;left:'+xpos+'px;top:'+ypos+'px;" ><div class="tagged_box" style="width:'+pos_width+'px;height:'+
        pos_height+'px;display:none;" ></div>')
         }
});   //END OF SPAN.CONNECTION ITERATION
});

编辑 所以我将 id 更改为 classes,现在标签显示在每张照片上。成功!然而,它仍然显示他们两个,而不是他们受人尊敬的标签。我认为这与 .each() 方法有关。

编辑 #2 最新代码

该块遍历 2 个图像。 现在 .tagged 会显示在两张图片上,而不是每个受尊重的图片一个标签

<div class="container">
<% if @new_manual.present? %>
<% @new_manual.steps.each do |step| %>
    <% i_connection = Contact.find(step.input_contact) %>

<span class="i_connection" data-pos-x="<%= i_connection.pos_x %>" data-pos-y="<%= i_connection.pos_y %>"  data-pos-width="<%= i_connection.pos_width %>" data-pos-height="<%= i_connection.pos_height %>"> </span>
<br>
<div class="image_panel">
    <%= image_tag(i_connection.image.image.url(:large)) %>
        <div class='planetmap'></div>
</div>
    <script type="text/javascript">
    $(document).ready(function(){
$("span.i_connection").each(function() {
    var pos_width = $(this).data('pos-width');
    var pos_height = $(this).data('pos-height');
    var xpos = $(this).data('pos-x');
    var ypos = $(this).data('pos-y');

    $(".tagged_box").css("display","block");
    $(".tagged").css("border","5px solid red");

    // if ((xpos !== undefined) && (ypos !== undefined)) {
    // console.log('X:' + xpos + 'px' + ' ' + 'Y:' + ypos +'px');         
        $('.planetmap').append('<div class="tagged"  style="width:'+pos_width+'px;height:'+pos_height+'px;left:'+xpos+'px;top:'+ypos+'px;" ><div class="tagged_box" style="width:'+pos_width+'px;height:'+
            pos_height+'px;" ></div>')
         // }
});   //END OF SPAN.CONNECTION ITERATION
});
</script>   

<% end %>   
<% end %>

【问题讨论】:

  • 您能否发布从您的 ERB 块生成的 HTML,如果问题出在 jQuery.each(),那么人们会更容易弄清楚发生了什么。

标签: jquery image iterator html


【解决方案1】:

很难从您提供的代码中了解最终生成的 HTML 的样子,但跳出的一个错误是您的 span 有一个 "i_connection" 类,而您的 jQuery 选择器正在寻找一个带有"connection" 的类。这实际上应该会导致 .each() 根本无法运行,所以这可能不是您的全部问题。

【讨论】:

    【解决方案2】:

    我在您的代码中看到的几个问题:

    1. 您正在设置 &lt;span class="i_connection",但使用 Jason 已经指出的 $("span.connection").each(function() {
    2. 您有多个divs 和相同的id,即image_panelplanetmapids 在整个 DOM 文档中应该是唯一的。

    要解决第一个问题,您应该更改 $("span.connection").each(function() { 以使用正确的类,即

    $("span.i_connection").each(function() {
    

    我假设您希望 image_panel div 仅在 &lt;% if @new_manual.present? %&gt; 评估为真时显示。这解决了重复的id 问题之一。对于planetmap 上的第二个重复ID,我再次假设您希望它在&lt;% @new_manual.steps.each do |step| %&gt; 之外,基于我看到的JS 代码。请按如下方式更新您的 erb,看看这是否是您想要的:

    <% if @new_manual.present? %>
      <div class='image_panel'>
        <% @new_manual.steps.each do |step| %>
          <% i_connection = Contact.find(step.input_contact) %>
          <span class="i_connection" data-pos-x="<%= i_connection.pos_x %>" data-pos-y="<%= i_connection.pos_y %>"  data-pos-width="<%= i_connection.pos_width %>" data-pos-height="<%= i_connection.pos_height %>"> </span>  
          <br>
    
          <%= image_tag(i_connection.image.image.url(:large)) %>
        <% end %>
        <div id='planetmap'></div>
      </div>
    <% end %>
    

    【讨论】:

    • 所以我修复了 1. 和 2. 并将我的 ERB 更新为您建议的内容,但是第一个图像有标签但第二个没有。另外,如果我将 erb 更改为:
      第二个标签显示,但第一个不显示
    • @EricFilkins,当您说tag 时,您指的是html elements 还是您的课程?我看到的另一个问题是,您的 javascript 中只有一个关闭 div &lt;/div&gt; 用于两个打开的 div &lt;div&gt;。您在哪里添加图像,请同时发布该代码。此外,最好发布您的最新代码,以便我们获得更好的图片。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签