iframe 自动适应高度
2010-06-16 14:09
337 查看
以下是iframe 实现自适应高度的DEMO
//////////////iframeResize.js内容/////////////////
function iframeAutoFit()
{
try{
if(window!=parent){
var a = parent.document.getElementsByTagName("IFRAME");
for(var i=0; i<a.length; i++) {
if(a[i].contentWindow==window){
var h1=0, h2=0, d=document, dd=d.documentElement;
a[i].parentNode.style.height = a[i].offsetHeight +"px";
a[i].style.height = "10px";
if(dd && dd.scrollHeight) h1=dd.scrollHeight;
if(d.body) h2=d.body.scrollHeight;
var h=Math.max(h1, h2);
if(document.all){h += 4;}
if(window.opera){h += 1;}
a[i].style.height = a[i].parentNode.style.height = h +"px";
}
}
}
}
catch (ex){}
}
if(window.attachEvent){
window.attachEvent("onload", iframeAutoFit);
}
else if(window.addEventListener){
window.addEventListener('load', iframeAutoFit, false);
}
///////////////iframeResize内容完////////////////////
<html>
<head>
<title>iframe内的页面命名为child.html,内容如下:</title>
<script src="js/iframeResize.js" type="text/javascript" ></script>
</head>
<body>
<div>
<table border="0" width="200" style="height: 400px; background-color: black">
<tr>
<td></td>
</tr>
</table>
</body>
</html>
////////////////////////拥有iframe的页面为index.html,内容如下://///////////////////
<html>
<head></head>
<body>
<div><iframe src="child.html" frameBorder="0" scrolling="no"></iframe></div>
</body>
</html>
//////////////iframeResize.js内容/////////////////
function iframeAutoFit()
{
try{
if(window!=parent){
var a = parent.document.getElementsByTagName("IFRAME");
for(var i=0; i<a.length; i++) {
if(a[i].contentWindow==window){
var h1=0, h2=0, d=document, dd=d.documentElement;
a[i].parentNode.style.height = a[i].offsetHeight +"px";
a[i].style.height = "10px";
if(dd && dd.scrollHeight) h1=dd.scrollHeight;
if(d.body) h2=d.body.scrollHeight;
var h=Math.max(h1, h2);
if(document.all){h += 4;}
if(window.opera){h += 1;}
a[i].style.height = a[i].parentNode.style.height = h +"px";
}
}
}
}
catch (ex){}
}
if(window.attachEvent){
window.attachEvent("onload", iframeAutoFit);
}
else if(window.addEventListener){
window.addEventListener('load', iframeAutoFit, false);
}
///////////////iframeResize内容完////////////////////
<html>
<head>
<title>iframe内的页面命名为child.html,内容如下:</title>
<script src="js/iframeResize.js" type="text/javascript" ></script>
</head>
<body>
<div>
<table border="0" width="200" style="height: 400px; background-color: black">
<tr>
<td></td>
</tr>
</table>
</body>
</html>
////////////////////////拥有iframe的页面为index.html,内容如下://///////////////////
<html>
<head></head>
<body>
<div><iframe src="child.html" frameBorder="0" scrolling="no"></iframe></div>
</body>
</html>
相关文章推荐
- 父页面iframe高度自动适应子页面iframe高度
- iframe 跨域自动适应高度
- 兼容火狐和IE的javascript中iframe自动适应高度
- Iframe自动适应宽度和高度(both IE and Firefox are ok)
- 用JS控制Iframe自动适应页面的高度
- Iframe自动适应高度
- Iframe自动适应宽度和高度
- iframe的用法,以及如何让iframe自动适应父及的宽度和高度
- Iframe自动适应宽度和高度(both IE and Firefox )
- Iframe 自动适应页面的高度示例代码
- iframe 自动适应高度
- iframe自动适应高度及iframe中含有页签的高度动态适应方法
- iframe 自动适应子页面高度宽度
- 用iframe 自动适应高度
- 实现IFRAME自动适应高度
- Iframe自动适应高度
- iframe高度自动适应
- iframe自动适应加载页面高度
- Iframe 自动适应页面的高度
- js Iframe 自动适应 高度 宽度