0

我被要求根据内容调整 iframe 的大小。经过长时间的搜索,我终于让它根据需要工作,但它只适用于 Firefox。我需要它至少在 chrome、ff 和 ie 上工作。这是脚本

<script language="JavaScript">
<!--
function autoResize(id){
    var newheight;

    if(document.getElementById){
        newheight=document.getElementById(id).contentWindow.document.body.scrollHeight; //ff
        newheight=document.getElementById(id).contentDocument.body.offsetHeight; //chrome
        newheight=document.getElementById(id).contentDocument.documentElement.scrollHeight;

        }

    document.getElementById(id).height= (newheight) + "px";
}
//-->
</script>

和 iframe 代码

<iframe name="result" id="iframe" seamless src="action/search.php" width="100%" onLoad="autoResize('iframe');" marginwidth=0 marginheight=0 frameborder=0 scrolling="no" style="border:0; overflow:hidden">
    </iframe>

任何建议表示赞赏。

4

2 回答 2

0

尝试这个

onload="if (window.parent &amp;&amp; window.parent.autoIframe) {window.parent.autoIframe('tree');}

在 iframe 和

<script type="text/javascript">
    function autoIframe(frameId){
        try{
            frame = document.getElementById(frameId);
            innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;

            if (innerDoc == null){
                            // Google Chrome
                frame.height = document.all[frameId].clientHeight + document.all[frameId].offsetHeight + document.all[frameId].offsetTop;
            }
                    else{
                    objToResize = (frame.style) ? frame.style : frame;
                    objToResize.height = innerDoc.body.scrollHeight + 18;
                    }
        }

        catch(err){
                alert('Err: ' + err.message);
            window.status = err.message;
        }
    }
    </script>
于 2012-11-05T03:01:30.727 回答
0

与其直接在元素上设置高度,不如将其设置为属性:

var heightAttr = document.createAttribute("height");
heightAttr.value = newheight;
document.getElementById(id).attributes.setNamedItem(heightAttr);

或者您可以在 css 对象中设置它:

document.getElementById(id).style.height = (newheight) + "px";
于 2012-11-05T03:05:57.380 回答