如何删除底部的额外iframe空间?

时间:2015-02-04 21:07:25

标签: javascript html css iframe

这是我关于stackoverflow的第一个问题。我想要做的是用javascript创建一个iframe到test div并在其中添加html代码。另外,我想动态调整iframe的高度。只有当我将dynamic_div的高度设置为150px以上时,下面的代码才对我有用。如果dynamic_div的高度小于150,它会自动将iframe的高度设置为150.如何解决此问题?

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>

2 个答案:

答案 0 :(得分:0)

由于您的<div id="container">

自动它不会像你的iframe一样高。 添加一些风格。

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

答案 1 :(得分: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

相关问题