为什么我的div不出现?

时间:2013-07-22 18:01:36

标签: jquery ruby-on-rails image iteration

所以我创建了一个erb块,它遍历一组图像,然后在给定的坐标处为每个图像显示div.tagged。在这种特殊情况下,块会迭代1个图像,但div.tagged不会显示在页面上。当我检查元素.tagged在那里,但实际上没有显示。任何人都有任何想法为什么? Here is the HTML that is generated. And here is a screen shot of the inspected element

ERB:

<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 %>" data-image=> </span>
<br>

<div class="image_panel">
    <%= image_tag(i_connection.image.image.url(:large)) %>
<div class='i_connection'></div>
</div>

<% end %>   
<% end %>

</div>

jQuery的:

<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");


        $('.i_connection').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;" style="position:absolute;"></div>')
}); 
});

CSS:

.i_connection div{
  display:block;
  position:absolute;
}

.image_panel{
  float:left;
  width:600px;
  position:relative;
}
.image_panel img{
  left:0;top:0px;
  max-width: 600px;
  overflow: hidden;
}

1 个答案:

答案 0 :(得分:0)

检查this;相同的jsfiddle,第5次迭代。

.append()中使用的HTML字符串未正确嵌套。缺少结束</div>标记。此外,担心会有一些报价不平衡。我决定使用jQuery替代方法来构建HTML。

我以为你想在.tagged_box中嵌套.tagged。如果这个假设是错误的,你应该改变

_tagged.append(_tagged_box);
$('div.i_connection').append(_tagged);

...到...

$('div.i_connection').append(_tagged_box);
$('div.i_connection').append(_tagged);

......按照你想要的顺序。

p.s:您可能希望为内部.i_connection寻找另一个类名,因为jQuery seector与父span和内部div匹配。

干杯。

相关问题