DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> JQuery自適應IFrame高度(支持嵌套 兼容IE,ff,safafi,chrome)
JQuery自適應IFrame高度(支持嵌套 兼容IE,ff,safafi,chrome)
編輯:JQuery特效代碼     
代碼如下:
function adjustIFramesHeightOnLoad(iframe) {
var iframeHeight = Math.min(iframe.contentWindow.window.document.documentElement.scrollHeight, iframe.contentWindow.window.document.body.scrollHeight);
$(iframe).height(iframeHeight);
}

失敗的測試就不說了,來直接的。
兩個鏈接和iframe:
代碼如下:
<li><a href="selfinfo.jsp" target="c-c-iframe" title="個人信息" >個人信息</a></li>
<li><a href="modifypass.jsp" target="c-c-iframe" title="修改密碼" >修改密碼</a></li>
<iframe src="init.jsp" id="c-c-iframe" name="c-c-iframe" width="500px;" frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe>

js代碼:
代碼如下:
<script type="text/javascript">
<!--
$(function(){
$("#c-c-iframe").load(function(){
$(this).height($(this).contents().find("#content").height() + 40);
});
});
-->
</script>

這裡的find("#content")是找出iframe內容文檔中的id為content的高度(另外比如find("body")),並設置給iframe,
類似的還可以設置寬度,留給需要的朋友嘗試吧。
這樣就解決了iframe不會因為內容過大被擋住的問題(因為我設置了scrolling="no")。
PS:基本上我會優先考慮使用iframe來實現無刷新,兼容浏覽器的後退按鈕;而且使用iframe加載flash是很爽的,不用寫什麼js調用,object標簽,還符合W3C標准。
2008年11月28日17:13:31 ,今天使用過程中根據實際情況進行了一下改良,代碼如下:
代碼如下:
<script type="text/javascript">
<!--
$(function(){
$("#workArea").load(function(){
var height = $(this).contents().find("#box").height() + 40;
//這樣給以一個最小高度
$(this).height( height < 400 ? 400 : height );
});
});
-->
</script>

另發現使用find("body")不太好使,高度不准確。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved