在表格里放了一个自适应高度的iframe,这个自适应高度的iframe单独运行的时候都正常,放到另外一个网页的表格里之后却不能正常自适应高度了。
--------------------------------------------------------------------------
自适应高度的iframe页面代码如下:
<script type="text/javascript">// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
function setIframe(){
var iframe = document.getElementById("iframeB");
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = bHeight>dHeight?bHeight:dHeight;
iframe.height = height;
}
// ]]></script>
<iframe width="100%" name="iframeB" id="iframeB"  src="test.asp.htm" marginwidth="0" marginheight="0" frameborder="0" onload="Javascript:setIframe()" scrolling="no"></iframe> 
-------------------------------------------------------------------------------

解决方案 »

  1.   

    首先,代码IE only。其次加<!DOCTYPE html。。>检查与否的写法也不合理.<script type="text/javascript">
    function setIframe(){
      var iframe = document.getElementById("iframeB");  
      var doc=iframe.contentDocument||iframe.contentWindow.document;//浏览器兼容  
      var docE=document.compatMode=="CSS1Compat"?document.documentElement:document.body;//DOCTYPE 兼容
      /*
        var bHeight = iframe.contentWindow.document.body.scrollHeight;
        var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
      */
      var sH = docE.scrollHeight;
      iframe.style.height = sH +'px';
    }
    </script>
      

  2.   

    表格没设定高度吧 。ie里元素会自适应。fifox就不会了
      

  3.   

    表格就是这样:<td><iframe width="100%" name="iframeB" id="iframeB" src="test.asp.htm" marginwidth="0" marginheight="0" frameborder="0" onload="Javascript:setIframe()" scrolling="no"></iframe>  </td>