jquery庫實現iframe自適應內容高度和寬度


javascript原生和jquery庫實現iframe自適應內容高度和寬度---推薦使用jQuery的代碼! 

‍<iframe src="index.php" id="mainiframe" name="mainiframe" width="100%"   frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe> 
基於Jquery庫的代碼很好實現: 
<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原生代碼的實現: 
<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庫實現iframe自適應內容高度和寬度


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM