【发布时间】:2014-05-08 17:42:45
【问题描述】:
我使用 jQuery 创建了一个弹出窗口,当弹出窗口包含路径时,我包含一个 IFRAME 作为弹出窗口的内容。代码调整 IFRAME 的大小以占用该 IFRAME 标记的父 DIV 中的全部可用空间。
相关代码如下:
jQuery("<div class='snap-popup' id='"
+ popup.id
+ "' style='position:fixed;display:none;'><div class='close-popup'>"
+ "</div><div class='inside-popup'><div class='popup-title'></div>"
+ "<div class='popup-body'></div></div></div>")
.appendTo("body");
popup.widget = jQuery("#" + popup.id);
i = popup.widget.children(".inside-popup");
b = i.children(".popup-body");
[...snip...]
if(popup.path)
{
b.empty();
b.append("<iframe class='popup-iframe' src='" + popup.path + "' frameborder='0'"
+ " marginheight='0' marginwidth='0'></iframe>");
f = b.children(".popup-iframe");
f.attr("width", b.width());
f.attr("height", popup.widget.offset().top + popup.widget.height()
- b.offset().top);
}
whole code 可以在 SourceForge.net 上找到。生成的 HTML 如下所示(为清楚起见,添加了换行符和缩进):
<div class="snap-popup" id="create-finball" style="position: fixed;
width: 500px; height: 300px; top: 27px; left: 390px; display: block;
z-index: 3;">
<div class="close-popup"></div>
<div class="inside-popup">
<div class="popup-title"></div>
<div class="popup-body">
<iframe class="popup-iframe" src="/finball/company/create"
marginheight="0" marginwidth="0" frameborder="0"
height="265" width="500"></iframe>
</div>
</div>
</div>
再次重申:我在该 DOM 中没有任何换行符或 <br/> 标记。它实际上只是一个 DIV 和一个直接子 IFRAME。就是这样。
然而,当我检查 IFRAME 和它的 DIV 容器的大小时,我注意到高度不同。我有两个屏幕截图显示了 DIV 和 IFRAME 的尺寸。我们可以看到 DIV 比 IFRAME 高 4 个像素(请参见右下角的布局。)
DIV 中的这些额外像素有什么原因吗?当我在 Firebug 中动态改变 IFRAME 的高度时,DIV 也会改变它的高度:IFRAME 高度 + 4。
【问题讨论】:
-
@Malk,起初它似乎相关,尽管我没有尝试使用无缝。我尝试删除边框(CSS 中的
border:none),但这没有任何效果,因为我已经有了frameborder="0"属性。
标签: javascript jquery html css iframe