让iframe简单有效自适应高度

来源:互联网 发布:java 初始化错误异常 编辑:程序博客网 时间:2024/05/01 08:17

一种简单有效自适应高度的iframe


<iframe src="show.htm" id="show" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" width="100%" onload="this.height=0;var fdh=(this.Document?this.Document.body.scrollHeight:this.contentDocument.body.offsetHeight);this.height=(fdh>7?fdh:7)"></iframe>


注:onload字段中的数值“7”为Iframe最低高度值,可自定义!
另外这个同时支持IE&Firefox:)

PS:如果内页的CSS设置了BODY的MARGIN属性,IFRAME自适应的高度不会把margin-top和margin-bottom的值加到里面,所以最后这地方this.height=(fdh>450?fdh:400)得改成
this.height=(fdh>450?fdh+margin-top的高度+margin-bottom的高度:400)
比如说
this.height=(fdh>450?fdh+20+15:400) //尚未测试