iframe高度自适应不对,scrollHeight取得的值不正确

 function iFrameHeight2(p) {   
var ifm= document.getElementById(p);  
var subWeb = document.frames ? document.frames[p].document : ifm.contentDocument;   
alert(subWeb.body.scrollHeight  +(p));
if(ifm != null && subWeb != null) {

   ifm.height = subWeb.body.scrollHeight   ;
   ifm.width = subWeb.body.scrollWidth;
}   


 <div id="tabs-r">
            <div class="subtabs">
              <ul style="background:#FFF; border-color:#FFF; color:#FFF">
                <c:forEach items="${rescueList}" var="rescue" varStatus="i">
                    <li>
                        <a href="#subtabs-${i.count}" data-toggle="tab"> ${rescue.no}</a>
                    </li>
                </c:forEach>
              </ul>
              <c:forEach items="${rescueList}" var="rescue" varStatus="i">
                <div id="subtabs-${i.count}" style="margin:0;padding:0;overflow:hidden" >
                    <iframe id="${i.count}a"
                        src="${ctx}/business/act/rescue/toLook?id=${rescue.id }&&type=1";scrolling="no";
                        style="width: 100% ; margin: 0; padding: 0;" marginwidth="0" marginheight="0"; onLoad="iFrameHeight2('${i.count}a')">
                    </iframe>
                </div>
              </c:forEach>
            </div>
          </div> 

获取body的offsetHeight,scrollHeight会获取的是iframe加载的所有页面中的最高的

改这样试试


 function iFrameHeight2(p) {   
var ifm= document.getElementById(p);  
var bd=ifm.contentWindow.document.body;
///要注意加上px单位
   ifm.height =bd .offsetHeight+'px'  ;
   ifm.width  =bd .offsetWidth+'px'  ;
}   

不行,原来的方法是只有第一个的页面是正常的,剩下的得到的页面高度都太大。你的这个方法也是一样的问题

问一下,你是要在iframe中有多个页面切换?
如果是,你就要每次切换的时候给iframe重新设置高度,才能保证每次的高度都是对的

 var bHeight = iframe.contentWindow.document.body.scrollHeight;
            var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
            var height = Math.max(bHeight, dHeight);
            iframe.height = height;

可以给iframe外面的div高度宽度,iframe的高宽直接给100%