本文實例講述了javascript實現框架高度隨內容改變的方法。分享給大家供大家參考。具體如下:
有兩種方法:
一、就是通過父頁面改變
這裡要理解框架的兩個屬性 contentWindow 和contentDocument 兩個屬性的意思和window document意思差不多,不同的是contentWindow 所有浏覽器都支持,contentDocument ie6,7不支持,chrome 也不支持
<iframe onload="change_height()"></iframe>
function change_height(){
var iframe=document.getElementById("iframe_id");
//取得框架元素
var i_height=iframe.contentWindow.document.body.scrollHeight||iframe.contentWindow.document.documentElement.scrollHeight;
//取得框架內容的高度
iframe.height=i_height;
//改變
}
二、就是通過內容改變
在內容頁進行
window.onload=function(){
var iframe=parent.document.getElementById("iframe_id");
//取得框架元素
iframe.height=document.body.scrollHeight||document.documentElement.scrollHeight;
//取得框架內容的高度並改變
}
希望本文所述對大家的javascript程序設計有所幫助。