【问题标题】:why isn't my div showing up?为什么我的 div 没有出现?
【发布时间】:2013-07-21 14:00:21
【问题描述】:

所以我创建了一个遍历一组图像的 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;
}

【问题讨论】:

  • 您是说 div 标记未显示,但您正在提供其他 div 的 css 代码。我也会为 tagged div 放置 css
  • 您应该能够比我们通过阅读您的代码更容易地进行调试。使用 chrome 检查,您可以编辑 CSS 和 HTML 属性,玩转直到可以显示它,考虑z-index,也许?
  • 我把它放在jsfiddle 中,但我在你的span .i_connection 标签中看不到任何东西。
  • 标记的 css 在 jQuery 中的样式是正确的,我尝试使用 z-index 和绝对定位,仍然没有运气。
  • @Yasky 正是我的问题。如果您检查图像,.tagged 是否存在它只是没有显示

标签: jquery ruby-on-rails image iteration


【解决方案1】:

签出this;相同的 jsfiddle,第 5 次迭代。

.append() 中使用的 HTML 字符串未正确嵌套。缺少关闭 &lt;/div&gt;tag。此外,担心会出现一些报价不平衡。我决定使用 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 搜索器与父 span 和内部 div 匹配。

干杯。

【讨论】:

    猜你喜欢
    • 2020-10-25
    • 2021-07-30
    • 2015-09-04
    • 2013-03-15
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    相关资源
    最近更新 更多