做網站的許多時候需要調用iframe,但是調用iframe的高度寬度很難控制好,如果控制不好會比較難看,而且可能部分內容看不到,或者出現(xiàn)滾動條,本篇文章就主要講:如何通過javascript或者jquery實現(xiàn)iframe自適應內容高度和寬度。
javascript原生和jquery庫實現(xiàn)iframe自適應內容高度和寬度---361源碼網推薦使用jQuery的代碼!
<iframe src="index.php" id="mainiframe" name="mainiframe" width="100%" frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe>
基于Jquery庫的代碼很好實現(xiàn):
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#mainframe").load(function(){
$(this).height(0); //用于每次刷新時控制IFRAME高度初始化
var height = $(this).contents().height() + 10;
$(this).height( height < 500 ? 500 : height );
});
});
</script>
下面是基于JS原生代碼的實現(xiàn):
<script language="javascript">
if (window.parent.length>0){window.parent.document.all.mainframe.style.height=document.body.scrollHeight;}
</script>
只需在你被iframe調用的文件</body>之后加入上面這段即可!
這個也可以控制iframe的高度隨內容的多而自動增長
<iframe name="web" width="100%" frameborder=0 height="100%" src="index.php" id="web" onload="this.height=web.document.body.scrollHeight+20" ></iframe>
jquery庫實現(xiàn)iframe自適應內容高度和寬度。
是不是挺簡單的呢,嘿嘿
以上就是如何通過javascript或者jquery實現(xiàn)iframe自適應內容高度和寬度的全部內容,希望對大家的學習和解決疑問有所幫助,也希望大家多多支持武林網。新聞熱點
疑難解答
圖片精選