经典代码如下自动改变 Iframe 的高度:
funcon setIFrameHeight(iframe) {
var ifm_content = document.getElementById("conFrame");
ifm_content.height = "500px";//默认设置为 500px
var heightContent = "500px";
var subContent = document.frames ? document.frames["conFrame"].document :
ifm_content.contentDocument;
if (iframe == "content") { //如果传入 Ifame Id 等于 content 重新给 iframe 赋值高度
heightContent = Math.max(subContent.body.scrollHeight,
subContent.documentElement.scrollHeight);
ifm_content.height = heightContent;
}
}
实例说明:
我的 Html 中有一个 Iframe 用法如下:
<iframe id="conFrame" src="a.html" frameborder="0" scrolling="no"
width="100%" height="500px"
onload="setIFrameHeight('content');"></iframe>
- 1
- 2
- 3
- 4
- 5
- 6
前往页