当前位置:Gxlcms > JavaScript > jquery实现iframe自适应高宽的方法

jquery实现iframe自适应高宽的方法

时间:2021-07-01 10:21:17 帮助过:3人阅读

这次给大家带来jquery实现iframe自适应高宽的方法,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自适应内容高度和宽度

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

Jquery操作js数组及对象步骤详解

jquery从数组移除选中值步骤详解

以上就是jquery实现iframe自适应高宽的方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行