iframe自适应高度

来源:互联网 发布:互联网 加大数据 编辑:程序博客网 时间:2024/05/10 02:34

在做项目的过程中,需要用到iframe,但是iframe的高度如果写死的话,它对应的页面高度都得固定,这样不能适应页面内容变化,不太灵活,所以写了以下的方法,根据iframe对应的页面内容来决定iframe的高度。

这个本人测试适应于ie,火狐浏览器。

<iframe src="a.html" id="iframe"

frameBorder="0" marginheight="0" marginwidth="0" width="500px"
height="100%" scrolling="no" name="iframe"

onLoad="getHeight()"> </iframe>


<script type="text/javascript" language="javascript">
function getHeight() {
var ifm = document.getElementById("iframe");//iframe的id
var subWeb = document.frames ? document.frames["iframe"].document
:

ifm.contentDocument;//iframe的name

if (ifm != null && subWeb != null) {

ifm.height = subWeb.body.scrollHeight;

}
}
</script>
0 0
原创粉丝点击