首頁  >  文章  >  web前端  >  用js操作iframe改變介面高度

用js操作iframe改變介面高度

php中世界最好的语言
php中世界最好的语言原創
2018-04-17 16:46:402578瀏覽

這次帶給大家用js操作iframe改變介面高度,用js操作iframe改變介面高度的注意事項有哪些,下面就是實戰案例,一起來看一下。

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<input type="button" value="页面1"/>
<input type="button" value="页面2"/>
<iframe id="iframePage" src="1.html" frameborder="0" scrolling="no"></iframe>
</body>
<script>
//  contentWindow所有浏览器支持,contentDocument IE不支持;  window.parant当前页面的父页面,window.top当前页面的最外层页面
//  iframe防钓鱼:if(window!=window.top){window.top.location.href=window.location.href} 如果最顶层页面不是当前页面,就把最顶层网址指向当前页面网址
    window.onload=function(){
        var oIframe=document.getElementById('iframePage');
        var aBtn=document.getElementsByTagName('input');
        changeHeight();
        function changeHeight(){
            setTimeout(function(){  //必须加一个延迟,不然高度改变会和页面加载冲突,事先不了高度变化
                oIframe.height=oIframe.contentWindow.document.body.offsetHeight;
            },100);
        }
        aBtn[0].onclick=function(){
            oIframe.src='1.html';
            changeHeight();
        };
        aBtn[1].onclick=function(){
            oIframe.src='2.html';
            changeHeight();
        };
    };
</script>
</html>

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:



#

以上是用js操作iframe改變介面高度的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn