2015-02-04 51 views
1

这是我在stackoverflow上的第一个问题。我想要做的是创建一个iframe到JavaScript的测试div并添加html代码。另外我想动态调整iframe的高度。下面的代码仅适用于我设置dynamic_div的高度超过150像素的作品。如果dynamic_div的高度小于150,它会自动将iframe的高度设置为150.我该如何解决这个问题?如何删除底部额外的iframe空间?

P.S:html变量中的html代码是动态的。所以我不能改变任何东西。

很多谢谢。

<html> 
<head> 
</head> 
<body> 
<div id="test" style="border: 1px solid;"></div> 
<script text="text/javascript"> 
var html = "<html><head></head><body><div id=\"container\" style=\"text-align:center;padding:8px 0;background-color:#f0f0f0;border:1px dashed;\"> <div id=\"dynamic_div\" style=\"width:100%;height:50px\">Some Content</div></body></html>"; 

function ui_aa(element_id,content){ // create an iframe and add txt into it. 
    var iframe = document.getElementById(element_id).appendChild(document.createElement('iframe')), 
    doc = iframe.contentWindow.document; 
    iframe.style.cssText = "width:100%"; 
    iframe.frameBorder = 0; 
    iframe.scrolling= 'no'; 
    doc.open().write(content); 
    doc.close(); 
    doc.body.style.cssText = "margin:0px;padding:0px;height:100%"; 
    var height = doc.body.scrollHeight; 
    iframe.height = height; 
}; 
ui_aa('test',html); 
</script> 
</body> 
</html> 

回答

0

的iframe中得到了不明原因的高度值给我,给它一个高度值,如0的伎俩,将doc.body.style.cssText覆盖高度值100%,所以你不必担心。

function ui_aa(element_id,content){ // create an iframe and add txt into it. 
    iframe.height= '0'; 
}; 

JSFiddle

0

因为你<div id="container">

的自动它不会是因为你的IFRAME一样高。 添加一些样式。

#container { 
    height: 100%; 
    display: block; 
} 
+0

感谢您发表评论塔马斯但它没有工作。 –

+0

奇怪。我尝试了jsfiddle。然后对不起。 –

相关问题