iframe 自適應高度[在IE6 IE7 FF下測試通過]
更新時間:2009年04月13日 10:41:45 作者:
有時候我們需要讓我們的iframe自動適應高度,但多瀏覽器兼容性不好,大家不妨試下這個。
第一種方法:
<script type="text/javascript" language="javascript">
<!--
//調整 PageContent 的高度
function TuneHeight() {
var frm = document.getElementById("content01");
var subWeb = document.frames ? document.frames["content01"].document : frm.contentDocument;
if(frm != null && subWeb != null) {
frm.height = subWeb.body.scrollHeight;
}
}
//-->
</script>
<iframe id="content01" name="content01" frameBorder=0 scrolling=no src="main.html" width="100%"onLoad="TuneHeight()" ></iframe>
第二種方法:
js code:
//iframe自適應高度[在IE6 IE7下測試通過]
function reSetIframe(){
var iframe = document.getElementById("iframeId");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
}catch (ex){}
}
html:
<iframe src="" id="weather" name="weather" width="278" onload="reSetIframe()" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" border="0" framespacing="0"> </iframe>
復制代碼 代碼如下:
<script type="text/javascript" language="javascript">
<!--
//調整 PageContent 的高度
function TuneHeight() {
var frm = document.getElementById("content01");
var subWeb = document.frames ? document.frames["content01"].document : frm.contentDocument;
if(frm != null && subWeb != null) {
frm.height = subWeb.body.scrollHeight;
}
}
//-->
</script>
<iframe id="content01" name="content01" frameBorder=0 scrolling=no src="main.html" width="100%"onLoad="TuneHeight()" ></iframe>
第二種方法:
js code:
復制代碼 代碼如下:
//iframe自適應高度[在IE6 IE7下測試通過]
function reSetIframe(){
var iframe = document.getElementById("iframeId");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
}catch (ex){}
}
html:
復制代碼 代碼如下:
<iframe src="" id="weather" name="weather" width="278" onload="reSetIframe()" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" border="0" framespacing="0"> </iframe>
您可能感興趣的文章:
相關文章
JS實現(xiàn)定時任務每隔N秒請求后臺setInterval定時和ajax請求問題
這篇文章主要介紹了JS實現(xiàn)定時任務每隔N秒請求后臺setInterval定時和ajax請求 的相關資料,需要的朋友可以參考下2017-10-10Ant Design Pro 下實現(xiàn)文件下載的實現(xiàn)代碼
這篇文章主要介紹了Ant Design Pro 下實現(xiàn)文件下載的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12JavaScript模擬實現(xiàn)簡單的MVC的示例詳解
MVC是一種常見的軟件架構模式,MVC模式的目的是將應用程序的數(shù)據(jù)、用戶界面和控制邏輯分離,提高代碼的可維護性,可拓展性和可重用性。本文就來用用JS模擬實現(xiàn)一個簡單的MVC吧2023-04-04CodeMirror2 IE7/IE8 下面未知運行時錯誤的解決方法
最近用CodeMirror2作后臺的模板編輯器,在IE9、Firefox下面沒有問題。到了IE7、IE8下面,textarea里面的代碼就顯示不出來了。搜索了好多,終于找到原因2012-03-03淺談 Webpack 如何處理圖片(開發(fā)、打包、優(yōu)化)
這篇文章主要介紹了淺談 Webpack 如何處理圖片(開發(fā)、打包、優(yōu)化),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05