重拾Javascript(四) iframe

动态计算IFRAME的高度

window.onload=function(){
        var oInput=document.getElementById('input1');
        var oIframe=document.getElementById('iframe1');
        oInput.onclick=function(){
            oIframe.contentWindow.document.getElementById('div1').style.color='red';
        };
 
        var oIframe2=document.getElementById('iframe2');
        function changeIfHeight(){
            //为什么这里要使用一个定时器,
            //是由于如果在改变Iframe的SRC属性修改 直接调用changeIfHeight方法 会不能直接反应iframe的高度
            //加个延时可以进行改变高度
            setTimeout(function(){
                oIframe2.height=oIframe2.contentWindow.document.body.offsetHeight;
            },100);
        }
        changeIfHeight();
        var oInput2=document.getElementById('input2');
        oInput2.onclick=function(){
            oIframe2.src='iframe3.html';
            changeIfHeight();
        }
    }
posted @ 2013-06-10 17:23  zhu_xj  阅读(127)  评论(0编辑  收藏  举报